代码之家  ›  专栏  ›  技术社区  ›  Matchu

Will I trigger a change event if I use Javascript to set a <option> as selected?

  •  4
  • Matchu  · 技术社区  · 14 年前

    我注意到,在Google Chrome中,设置 <option> as selected via Javascript does not trigger the respective <select> tag's change event.

    Can I count on this behavior holding true in all other modern browsers?

    4 回复  |  直到 14 年前
        1
  •  1
  •   Babiker    14 年前

    Setting an option as selected via JavaScript 通过聚焦选择一个选项,然后使用上下按钮进行选择,不被 onchange() event handler as evident by the following links :




    PS: A similar situation occurred when i tried to evoke <element onmousedown="doStuff();"> 通过呼叫 element.click();

        2
  •  1
  •   scunliffe    14 年前

    Setting any value for any field in JavaScript should not trigger any events.

    If they did you would end up in an endless loop of events firing.

    If you want, you can "call" the change event manually.

        3
  •  0
  •   Ciaran Archer    14 年前

    Yes, that should hold true. In my experience this works in IE 6 up, Chrome, FF and Safari. I would agree that testing in all browsers you intend supporting is sensible though !

        4
  •  0
  •   CarterMan    14 年前

    option:selected yes it works I just used it myself. Even held true in IE 6 - IE 8 webkit and FF