Javascript 选择框的onchange事件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10122047/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 23:56:55  来源:igfitidea点击:

onchange event of select box

phpjavascripthtml

提问by Priyanka

I am using a selectbox to display the list of "enterpirses" from the database. But the onchange event is not triggering. But when i manually once put the value of querystring then it starts working. I dont understand why it is happening.

我正在使用选择框来显示数据库中的“企业”列表。但是 onchange 事件没有触发。但是当我手动输入查询字符串的值时,它就会开始工作。我不明白为什么会这样。

I am new to javascript and php, kindly suggest me the solution to this.

我是 javascript 和 php 的新手,请建议我解决这个问题。

<select id="enterprisebox" onchange="javascript:valueselect()" >
    <option value="-">-</option>

    <?php foreach($enterprise as $val) { ?>
    <option value="<?php echo $val['customer_id'];?>"><?php echo $val['customer_name']?>
    </option>
    <? } ?>

</select>

and my javascript is--

而我的 javascript 是——

function valueselect()
{
    var i = document.getElementById('enterprisebox');
    var p = i.options[i.selectedIndex].value;
    //alert(p);
    window.location.href = "channelinformation.html?selected="+p;
}

回答by Ravi Jethva

onchange="javascript:valueselect()"replace with onchange="valueselect(this.value);"

onchange="javascript:valueselect()"用。。。来代替 onchange="valueselect(this.value);"

function valueselect(myval)
{
      window.location.href = "channelinformation.html?selected="+myaval;
}

You Can Use Directly

可以直接使用

onchange="window.location='channelinformation.html?selected='+myaval"

回答by sbhomra

Make life easier and use jQuery:

让生活更轻松并使用 jQuery:

$(document).ready(function () {  
    $('#enterprisebox').change(function () {
       window.location.href = "channelinformation.html?selected="+ $(this).val();
    });    

});

回答by GoSmash

Try this,

尝试这个,

<select id="enterprisebox" onchange="javascript:valueselect(this)" >
    <option value="-">-</option>

    <?php foreach($enterprise as $val) { ?>
    <option value="<?php echo $val['customer_id'];?>"><?php echo $val['customer_name']?>
    </option>
    <? } ?>

</select>

<script>
   function valueselect(sel) {
      var value = sel.options[sel.selectedIndex].value;
      window.location.href = "channelinformation.html?selected="+value;
   }
</script>