简体   繁体   English

如何使用Jquery(非委托)访问动态创建的tr中的复选框

[英]How can I access Checkboxes in a dynamically created tr using Jquery (Not delegation)

I have a table in which a tr can by dynamically added. 我有一个可以动态添加tr的表。 The tr contains checkboxes that signify whether a team member has a certain role - this is defined by having a utilisation greater than 0 for that role. tr包含表示团队成员是否具有特定角色的复选框-这是通过该角色的利用率大于0来定义的。 The roles and utilisation are set in a dialog box.The following method is called for each different role type when the dialog box values are saved against the team member. 在对话框中设置角色和使用率。针对团队成员保存对话框值时,将为每种不同的角色类型调用以下方法。

if (teamMember.designerUtil > 0) {
  $('tr', '#my-table').eq(tableRow).find("#designerCheck").prop("checked",true);
}

What I am trying to achieve is have the checkbox on the tr for the team member that has a role set in the modal check when they have been given the role. 我要实现的目标是,在为团队成员分配角色时,在其模式检查中设置角色的团队成员的tr上具有复选框。 The code above works only for the tr that was created when the page loads, NOT for any created dynamically. 上面的代码仅适用于页面加载时创建的tr,不适用于动态创建的tr。 I have read about event delegation but I don't think there is an event to attach. 我已经阅读了有关事件委派的内容,但我认为没有附加的事件。 I just want to check the checkbox using Jquery, not initiate an event. 我只想使用Jquery选中复选框,而不启动事件。

Image showing dialog box and table with checkboxes 该图显示了对话框和带有复选框的表

If by "dynamically added" you mean created by javascript when the page loads you should be able to execute your code using the document ready function: 如果“动态添加”是指在页面加载时由javascript创建,则您应该能够使用文档就绪功能执行代码:

https://learn.jquery.com/using-jquery-core/document-ready/ https://learn.jquery.com/using-jquery-core/document-ready/

If by "dynamically added" you mean created by an ajax call you should be able to use ajaxComplted to get the behavior you're looking for. 如果“动态添加”是指由ajax调用创建,则您应该能够使用ajaxComplted来获取所需的行为。

http://api.jquery.com/ajaxcomplete/ http://api.jquery.com/ajaxcomplete/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM