用原型观察多个选择菜单 [英] observing multiple select menus with prototype

查看:47
本文介绍了用原型观察多个选择菜单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想观察多个选择菜单并使用原型响应它们的更改,但似乎只观察到第一个菜单.这是我的代码:

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

如果在 product_options 中有 - 例如 - 3 个选择,那么它只观察第一个,我认为可能是因为调用,所以我尝试了这个:

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

还是不行,有什么想法有什么问题吗?

选择器绝对没有问题,因为 console.debug() 向我展示了所有的选择菜单

解决方案

你检查过你的 JS 错误控制台了吗?以下工作正常:

<头><title>optchange</title><script type="text/javascript" src="prototype.js"></script><身体><div id="product_options"><select id="o0"><option>1</option><option>2</option></选择><select id="o1"><option>1</option><option>2</option></选择><select id="o3"><option>1</option><option>2</option></选择>

<script type="text/javascript">函数 optchange(e) {警报(选择更改");}$('product_options').select('select').invoke("observe","change", optchange);</html>

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

解决方案

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>

这篇关于用原型观察多个选择菜单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆