从下载onchange重定向获取值

时间:2013-01-24 04:46:44

标签: php javascript redirect drop-down-menu onchange

我从下载onchange重定向,当我选择一个值,页面时,我重定向不改变值从下拉onchange redirct。 我的javascript

<script language="JavaScript" type="text/javascript">
                    function redirect(site){
                        window.location= site
                    }
                </script>
在我的表单上

就像这样

<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>

我如何从这个表格中选择值?

2 个答案:

答案 0 :(得分:1)

根据我的评论,您试图在URL中的最后一个参数上选择当前选项。你只需要selected="selected"。试试这个:

<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>

答案 1 :(得分:0)

您编写的脚本运行正常。 我可以假设页面上的URL或其他js代码有问题。 如果您使用的是不带协议的URL(例如http://),则可能会导致重定向问题。

如果您想获得一个类别,就像您在评论中指出的那样。然后我建议:

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