原型函数用于在复选框中设置onclick事件观察器

xai*_*ain 4 checkbox events click prototypejs

我想为以下每个复选框设置一个观察者:

<div id="listr">
  <ul id="lr" style="list-style-type: none">
    <p class="tir">Text 1</p>
    <li class="ro"><input type="checkbox" name="r1" id="r1"/>A1</li>
    <li class="ro"><input type="checkbox" name="r2" />A2</li>
    <p class="tir">Text 2</p>
    <li class="rubro"><input type="checkbox" name="r3" />B1</li>
    <p class="tir">Text 3</p>
    <li class="ro"><input type="checkbox" name="r4" />B2</li>
  </ul>
</div>
Run Code Online (Sandbox Code Playgroud)

如果我为每个复选框写一个观察者,它可以工作,但我想以一种简短的方式做,所以我需要类似的东西

$$('listr.lr.li.input').invoke('observe',click,function(field) {
    alert(this.name + ' clicked ' + this.checked);
    // other stuff ...
});
Run Code Online (Sandbox Code Playgroud)

哪个不行

提前致谢

kar*_*m79 7

尝试使用有效的CSS3选择器:

$$('#listr input[type="checkbox"]').invoke('observe','click',function(field) {
    alert(this.name + ' clicked ' + this.checked);
    // other stuff ...
});
Run Code Online (Sandbox Code Playgroud)

http://prototypejs.org/doc/latest/dom/dollar-dollar/