动态地添加点击事件复选框,只适用于第一个复选框JQUERY [英] Dynamically adding click event to checkbox, only works for first checkbox JQUERY

查看:126
本文介绍了动态地添加点击事件复选框,只适用于第一个复选框JQUERY的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,我一对夫妇的ascx在每个控制不同的复选框地区工作。
我最初使用

  $('[类型=复选框]')点击(函数(){code});

和它做的工作,在第一。但是,当我增加了一个控制,也使用复选框,该功能被用于发射其他复选框,以及让我升级了JS来

  $('#CHCK')点击(函数(){code});

但有了这个,只用火在该列表中的第一个复选框的事件..我怎样才能得到它火的所有点击?

我也试过这样的:

  $('#CHCK')每个(函数(){$(本)。点击({函数(){code});});

和这个只是工作的第一个以及

这是我的ascx页面例如:

 < ASP:直放站ID =接触=服务器>
            <&ItemTemplate中GT;
                &所述; TR>
                    &所述; TD>
                        <输入类型=复选框ID =CHCK/>
                    < / TD>
                  .....
                 < / TR>
             < / ItemTemplate中>
  < / ASP:直放站>

和这里是我JS code段

  $(文件)。就绪(函数(){
       $(#CHCK)。每个(函数(){
           $(本)。点击(函数(){的onchange($(本));});
       });
    });
    函数的onchange(复选框){
    //添加联系人列表
    }


解决方案

ID s必须是唯一的。使用一个类来代替:

 <输入类型=复选框级=CHCK/>

  $('。CHCK')点击(函数(){code});

So I am working with a couple ascx with different checkbox areas on each control. I originally used

 $('[type=checkbox]').click ( function(){ code });

and it did the job, At first. But when i added another control, also using checkboxes, the function was firing for the other checkboxes as well so i upgraded the js to

 $('#chck').click(function(){ code });

But with this, it only fires an event for the first checkbox in that list.. how can i get it to fire for all clicks?

I have also tried this:

 $('#chck').each(function(){ $(this).click({ function() { code }); });

and this only worked for the first one as well

here is my ascx page example:

 <asp:Repeater ID="contacts" runat="server">
            <ItemTemplate>
                <tr>
                    <td>
                        <input type="checkbox" id="chck" />
                    </td>
                  .....
                 </tr>
             </ItemTemplate>
  </asp:Repeater>

and here is my JS code segment

    $(document).ready(function(){
       $("#chck").each(function () {
           $(this).click(function () { onchange($(this)); });
       });
    });
    function onchange(checkbox) {
    // adds contact to list
    }

解决方案

ids must be unique. Use a class instead:

<input type="checkbox" class="chck" />

$('.chck').click(function(){ code });

这篇关于动态地添加点击事件复选框,只适用于第一个复选框JQUERY的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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