get value from drop down onchange redirect

时间秒杀一切 提交于 2021-01-29 08:18:35

问题


i have from drop down onchange redirect, when i select one of value, page when i redirect not changet value from drop down onchange redirct. my javascript

<script language="JavaScript" type="text/javascript">
                    function redirect(site){
                        window.location= site
                    }
                </script>

on my form like this

<select name="bulan" onchange="redirect(this.value)">
                    <option value="#">Pilih Bulan</option>
                    <option value="<?= base_url(); ?>index.php/kalender/cari_agenda/<?= $this->uri->segment(3); ?>/01">Januari</option>
                    <option value="<?= base_url(); ?>index.php/kalender/cari_agenda/<?= $this->uri->segment(3); ?>/02">Februari</option>
                    <option value="<?= base_url(); ?>index.php/kalender/cari_agenda/<?= $this->uri->segment(3); ?>/03">Maret</option>
                    <option value="<?= base_url(); ?>index.php/kalender/cari_agenda/<?= $this->uri->segment(3); ?>/04">April</option>
                    <option value="<?= base_url(); ?>index.php/kalender/cari_agenda/<?= $this->uri->segment(3); ?>/05">Mei</option>
                    <option value="<?= base_url(); ?>index.php/kalender/cari_agenda/<?= $this->uri->segment(3); ?>/06">Juni</option>
                    <option value="<?= base_url(); ?>index.php/kalender/cari_agenda/<?= $this->uri->segment(3); ?>/07">Juli</option>
                    <option value="<?= base_url(); ?>index.php/kalender/cari_agenda/<?= $this->uri->segment(3); ?>/08">Agustus</option>
                    <option value="<?= base_url(); ?>index.php/kalender/cari_agenda/<?= $this->uri->segment(3); ?>/09">September</option>
                    <option value="<?= base_url(); ?>index.php/kalender/cari_agenda/<?= $this->uri->segment(3); ?>/10">Oktober</option>
                    <option value="<?= base_url(); ?>index.php/kalender/cari_agenda/<?= $this->uri->segment(3); ?>/11">November</option>
                    <option value="<?= base_url(); ?>index.php/kalender/cari_agenda/<?= $this->uri->segment(3); ?>/12">Desember</option>
                </select>

how i get the value selected from this form ??


回答1:


The script that you written works just fine. I can assume that it is something wrong with URL or other js code on the page. If you are using URL without protocol (e.g. http://) then it may cause the problem for redirection.

If you want get a category, as you pointed it in your comment. Then I suggest this:

<script type="text/javascript">
function redirect(site){
    var category = site.split('/')[5];
    //other code goes here...
}
</script>



回答2:


According to the comments with me, you are trying to select the current option upon the last argument in the URL. You just need selected="selected". Try this:

<select name="bulan" onchange="redirect(this.value)">
    <option value="#">Pilih Bulan</option>
    <option value="<?= base_url(); ?>index.php/kalender/cari_agenda/<?= $this->uri->segment(3); ?>/01" <?php if($this->uri->segment(4) == '01') echo 'selected="selected"'; ?>>Januari</option>
    <option value="<?= base_url(); ?>index.php/kalender/cari_agenda/<?= $this->uri->segment(3); ?>/02" <?php if($this->uri->segment(4) == '02') echo 'selected="selected"'; ?>>Februari</option>
    <option value="<?= base_url(); ?>index.php/kalender/cari_agenda/<?= $this->uri->segment(3); ?>/03" <?php if($this->uri->segment(4) == '03') echo 'selected="selected"'; ?>>Maret</option>
    <option value="<?= base_url(); ?>index.php/kalender/cari_agenda/<?= $this->uri->segment(3); ?>/04" <?php if($this->uri->segment(4) == '04') echo 'selected="selected"'; ?>>April</option>
    <option value="<?= base_url(); ?>index.php/kalender/cari_agenda/<?= $this->uri->segment(3); ?>/05" <?php if($this->uri->segment(4) == '05') echo 'selected="selected"'; ?>>Mei</option>
    <option value="<?= base_url(); ?>index.php/kalender/cari_agenda/<?= $this->uri->segment(3); ?>/06" <?php if($this->uri->segment(4) == '06') echo 'selected="selected"'; ?>>Juni</option>
    <option value="<?= base_url(); ?>index.php/kalender/cari_agenda/<?= $this->uri->segment(3); ?>/07" <?php if($this->uri->segment(4) == '07') echo 'selected="selected"'; ?>>Juli</option>
    <option value="<?= base_url(); ?>index.php/kalender/cari_agenda/<?= $this->uri->segment(3); ?>/08" <?php if($this->uri->segment(4) == '08') echo 'selected="selected"'; ?>>Agustus</option>
    <option value="<?= base_url(); ?>index.php/kalender/cari_agenda/<?= $this->uri->segment(3); ?>/09" <?php if($this->uri->segment(4) == '09') echo 'selected="selected"'; ?>>September</option>
    <option value="<?= base_url(); ?>index.php/kalender/cari_agenda/<?= $this->uri->segment(3); ?>/10" <?php if($this->uri->segment(4) == '10') echo 'selected="selected"'; ?>>Oktober</option>
    <option value="<?= base_url(); ?>index.php/kalender/cari_agenda/<?= $this->uri->segment(3); ?>/11" <?php if($this->uri->segment(4) == '11') echo 'selected="selected"'; ?>>November</option>
    <option value="<?= base_url(); ?>index.php/kalender/cari_agenda/<?= $this->uri->segment(3); ?>/12" <?php if($this->uri->segment(4) == '12') echo 'selected="selected"'; ?>>Desember</option>
</select>


来源:https://stackoverflow.com/questions/14494153/get-value-from-drop-down-onchange-redirect

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!