onchange event of select box

2020-07-27 05:27发布

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.

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

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

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

3条回答
Rolldiameter
2楼-- · 2020-07-27 06:16

onchange="javascript:valueselect()" replace with 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"
查看更多
老娘就宠你
3楼-- · 2020-07-27 06:28

Make life easier and use jQuery:

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

});
查看更多
霸刀☆藐视天下
4楼-- · 2020-07-27 06:28

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>
查看更多
登录 后发表回答