observing multiple select menus with prototype

人走茶凉 提交于 2020-01-17 01:27:12

问题


I want to observe multiple select menus and respond to their changes using prototype but only the first menu seems to be observed. This is my code:

$('product_options').select('select').invoke("observe","change",optchange);

If there are - for example - 3 selects within product_options then it only observes the first, i thought it might be because of invoke so i then tried this:

$('product_options').select('select').each(function(sel){
    $(sel).observe("change",optchange);
});

Still doesnt work though, any ideas whats wrong?

There's definitely nothing wrong with the selector as a console.debug() shows me all the select menus


回答1:


Have you checked your JS error console? The below works just fine:

<html>
  <head>
    <title>optchange</title>
    <script type="text/javascript" src="prototype.js"></script>
  </head>
  <body>
    <div id="product_options">
      <select id="o0">
        <option>1</option>
        <option>2</option>
      </select>
      <select id="o1">
        <option>1</option>
        <option>2</option>
      </select>
      <select id="o3">
        <option>1</option>
        <option>2</option>
      </select>
    </div>
    <script type="text/javascript">
      function optchange(e) {
        alert("optchanged");
      }
      $('product_options').select('select').invoke("observe","change", optchange);
    </script>
  </body>
</html>


来源:https://stackoverflow.com/questions/2995777/observing-multiple-select-menus-with-prototype

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!