How to add an onchange event to a select box via javascript?

前端 未结 5 1387
醉酒成梦
醉酒成梦 2020-12-29 02:01

I\'ve got a script where I am dynamically creating select boxes. When those boxes are created, we want to set the onchange event for the new box to point to a

相关标签:
5条回答
  • 2020-12-29 02:08

    Add

    transport_select.setAttribute("onchange", function(){toggleSelect(transport_select_id);});
    

    setAttribute

    or try replacing onChange with onchange

    0 讨论(0)
  • 2020-12-29 02:09

    If you are using prototype.js then you can do this:

    transport_select.observe('change', function(){
      toggleSelect(transport_select_id)
    })
    

    This eliminate (as hope) the problem in cross-browsers

    0 讨论(0)
  • 2020-12-29 02:10
    yourSelect.setAttribute( "onchange", "yourFunction()" );
    
    0 讨论(0)
  • 2020-12-29 02:19

    Here's another way of attaching the event based on W3C DOM Level 2 Events Specification:

      transport_select.addEventListener(
         'change',
         function() { toggleSelect(this.id); },
         false
      );
    
    0 讨论(0)
  • 2020-12-29 02:23

    replace:

    transport_select.onChange = function(){toggleSelect(transport_select_id);};
    

    with:

    transport_select.onchange = function(){toggleSelect(transport_select_id);};
    

    on'C'hange >> on'c'hange


    You can use addEventListener too.

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