我试图在模态窗口内列出的复选框内使用jquery中的 .nextAll 选项。所以当用户单击链接并通过函数打开模态窗口时 .on("click") ,那就是 chexkbox出现并且我在 click 函数中编写了一个代码,它改变了复选框的 check 属性。
$('input[name="Company"],input[name="Country"]').live("click",function(){
if ($(this).is(':checked')){
alert("s");
$(this).nextAll('input[name="'+this.name+'Sub"]').prop('checked',true);
} else {
alert("n");
$(this).nextAll('input[name="'+this.name+'Sub"]').prop('checked',false);
}
});
<input type="checkbox" class="entityCheckboxHeader1" id="entityCheckboxHeader" name="Company">Company
<input class="modalEntityCompany" id="entity1" type="checkbox" name="CompanySub">Microsoft
<input class="modalEntityCompany" id="entity2" type="checkbox" name="CompanySub">Apple
<br />
<input type="checkbox" class="entityCheckboxHeader2" id="entityCheckboxHeader" name="Country">Country
<input class="modalEntity" id="entity3" type="checkbox" name="CountrySub">USA
<input class="modalEntity" id="entity4" type="checkbox" name="CountrySub">UK
当在 jsfiddle 或我们创建的新 html 中尝试时,该程序可以独立运行,但在我创建的 click 函数中使用它时它不会工作。什么可能导致冲突?
注意:我将 .live() 函数用于复选框,因为当使用 .on() 时它甚至不会进入 .nextAll 部分
动态创作部分
function generateTreeHTML(input) {
var html = '';
var entityNumber = 1;
var color = 663399;
html = "<ul id='tree' class='treeview-black' >";
for ( var m = 0; m < input.length; m++) {
html+="<li>";
currentEntity = input[m];
currentEntityType = currentEntity.entity;
html += "<span style='font-size:12px; font-family:Helvetica; font-weight:bold;' id='entityHeader' class='entityHeader"
+ getEntityHeader()
+ "'><input name='" + currentEntityType +"' type='checkbox' id='entityCheckboxHeader' class=\""
+ 'entityCheckboxHeader'
+ getEntityCheckboxHeader()
+ "\" />"
+ currentEntityType
+ "</span><div style='width:15px; height:10px; background-color:#"
+ colorArray[entityNumber]
+ "; float:right; margin-right:50px; margin-top:5px;'></div>";
html+="<ul>";
for ( var n = 0; n < currentEntity[currentEntityType].length; n++) {
html += "<li>";
var entityName = currentEntity[currentEntityType][n].entity;
var entityName1 = entityName.split('.').join("");
entityName1 = entityName1.split('_').join("");
entityName1 = entityName1.replace(/ /g, "");
html += "<span><input type='checkbox' key=\"" + entityName
+ "\" mapKey=\"" + currentEntityType
+ "\" categoryNumber=\"" + entityNumber
+ "\" class='modalEntity' id=\"" + 'entity' + getEntityID()
+ "\" name='" + currentEntityType + "Sub'>" + entityName + "</span>";
html += "</li>";
}
html+="</ul></li>";
entityNumber = entityNumber + 1;
html += "<div style='width:200px; border-bottom:1px solid #cccccc; height:1px;'></div>";
}
html += "</ul>";
return html;
}