dora1989
2013-09-11 11:02
浏览 54
已采纳

onchange事件未被触发

I have a simple select option box, the contents of which are generated from a PHP script. I looks like this

<form action="php/reloadNewList.php" method="POST">
    <select name="listToGo" onchange="redirect(this.form.value)">
    <?php 
    include('php/getMyList.php');
    getList(); 
    ?>
    </select>
</form>

The list generated looks fine

<option value="1">Hello</option>
<option value="12">Smelly</option>
etc

My JS script is simple enough as well

function redirect(value)
{
    var x=value.selectedIndex;
    alert("listToGo="+x + "
Value = "+value);
    document.cookie = "ListCookie="+x;
    window.location.reload();
}

The problem I'm getting is that the onchange is not responding to the change on the drop down list.

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • dro80463 2013-09-11 11:05
    已采纳

    Try using this -

    <select name="listToGo" onchange="redirect(this.value);">
    
    点赞 打赏 评论
  • dongshun7312 2013-09-11 11:08

    Try to change:

    <select name="listToGo" onchange="redirect(this)">
    

    And:

    function redirect(slct)
    {
        console.log(slct)
        var x=slct.selectedIndex;
        var value = slct.value;
        alert("listToGo="+x + "
    Value = "+value);
        document.cookie = "ListCookie="+x;
        window.location.reload();
    }
    

    JSFiddle: http://jsfiddle.net/cherniv/YyUwR/1/

    点赞 打赏 评论

相关推荐 更多相似问题