call javascript function onchange event of dropdown list

后端 未结 5 628
隐瞒了意图╮
隐瞒了意图╮ 2021-02-01 19:38

I want to invoke javascript function when value in the dropdown list changes. I dont want to hardcode dropdown list id .

Hence not using document.getElem

相关标签:
5条回答
  • 2021-02-01 19:49

    I don't know why do you need this onmousedown event here, but what you have to do is put your function above actual usage. Look at the snipplet below:

    <script type="text/javascript">
    function jsFunction(value)
    {
        alert(value);
    }
    </script>
    
    <select id ="ddl" name="ddl" onmousedown="this.value='';" onchange="jsFunction(this.value);">
      <option value='1'>One</option>
      <option value='2'>Two</option>
      <option value='3'>Three</option>
    </select>

    0 讨论(0)
  • 2021-02-01 19:52

    using jQuery

     $("#ddl").change(function () {
                    alert($(this).val());
                });
    

    jsFiddle

    0 讨论(0)
  • 2021-02-01 19:53

    You just try this, Its so easy

     <script>
    
      $("#YourDropDownId").change(function () {
                alert($("#YourDropDownId").val());
            });
    
    </script>
    
    0 讨论(0)
  • 2021-02-01 19:54

    jsFunction is not in good closure. change to:

    jsFunction = function(value)
    {
        alert(value);
    }
    

    and don't use global variables and functions, change it into module

    0 讨论(0)
  • 2021-02-01 20:11

    Your code is working just fine, you have to declare javscript method before DOM ready.

    your working example

    0 讨论(0)
提交回复
热议问题