How to edit 'onchange' attribute in a 'select' tag? (using jquery)

后端 未结 4 2101
清歌不尽
清歌不尽 2020-12-31 15:55

I\'m trying to edit the \'onchange\' event of an existent \'select\' element.

For example purposes I have the following code:


                        
    
提交评论

  • 2020-12-31 16:13

    Not an exact answer to the question, but I'd probably remove the event using this:

    document.getElementById('sel_id').onchange = undefined;
    

    (as seen at How to Clear/Remove JavaScript Event Handler? )

    and then go with this:

    $('#sel_id').change(function() { foo_2(); });
    
    0 讨论(0)
  • 2020-12-31 16:19

    Works for me if I use the native setAttribute().

    Also, remember that you need quotes around the selector.

    $("#sel_id")[0].setAttribute("onchange", "foo_2()");
    

    Also remember to define foo_2 in the global namespace, and not inside jQuery's .ready() function.

    0 讨论(0)
  • 2020-12-31 16:33

    Use JQuery change function.

    $('#sel_id').change(function() {
      //your code
    });
    
    0 讨论(0)
  • 提交回复
    热议问题