2012-07-11 69 views
0

我试图在模式窗口中列出的复选框内使用jquery中的.nextAll选项..所以当用户点击一个链接并打开模式窗口时,通过函数.on(“click”),那就是当chexkbox出现时,我在click函数中写了一个代码,它改变了复选框的检查属性。在的jsfiddle或者说我们创造,BT当我创建了一个点击功能内使用也不会WRK一个新的HTML内试图当.nextAll函数在点击函数中不起作用

$('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 

程序工作正常independly。什么可能导致冲突?

注:进出口使用.live()函数的复选框,因为它甚至不会获得在当。对()用于

动态创建部分

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; 
    } 
+0

做那些麦芽酒rts出现? ?关于你使用'.live()',你是否尝试将'.on()'代码放入文档就绪处理程序或脚本块中,该脚本块在尝试操作的复选框后出现? (或复选框在页面加载后一段时间动态添加?) – nnnnnn 2012-07-11 06:36:38

+0

复选框在模式窗口打开时动态添加,当使用.on时警报不会显示,使用.live()函数时会出现警报。 – user1371896 2012-07-11 06:40:43

+0

你是什么意思的“独立工作[但没有]我创建的点击功能”?您所说的点击功能之上的代码是行不通的?如果它在小提琴中起作用(并且它对我有用:http://jsfiddle.net/ez7cN/),它应该可以工作,假设您的动态添加复选框遵循该结构 - 您可以显示执行动态创建的代码吗? – nnnnnn 2012-07-11 06:41:29

回答

1

.nextAll() method只找到下一个兄弟元素(如在doco中非常清楚地陈述)。您正在动态创建的结构似乎将相关复选框分别放置在不同li元素内部的自身跨度中,即它们分别是而不是彼此的兄弟或您的点击处理程序所属的标题复选框。你不能指望你使用的遍历方法来找到不同的html结构的元素,而不仅仅是从我的办公室到我家的驾驶路线,这些方法都可以用于你的房子。

试试这个:

$('input[name="Company"],input[name="Country"]').live("click",function(){  
    if ($(this).is(':checked')){ 
     alert("s"); 
     $('input[name="'+this.name+'Sub"]').prop('checked',true); 
    } else { 
     alert("n"); 
     $('input[name="'+this.name+'Sub"]').prop('checked',false); 
    } 
}); 

这可以大大简化为:

$('input[name="Company"],input[name="Country"]').live("click",function(){  
     $('input[name="'+this.name+'Sub"]').prop('checked',this.checked);  
}); 

或者,如果您担心可能有同名的其他复选框您的文档在其他地方你可以看起来与点击的标题框所属的li元素相同:

$(this).closest('li') 
     .find('input[name="'+this.name+'Sub"]').prop('checked',this.checked); 
+0

这确实很漂亮...非常感谢.. – user1371896 2012-07-11 08:23:39

0

给一类.nextAll部分说checkinput到复选框input[name="Company"] and input[name="Country"]"

$('body').delegate(".checkinput","click",function(){ 

    if ($(this).is(':checked')){ 
     alert("s"); 
      $(this).nextAll('input[name="'+$(this).attr('name')+'Sub"]').prop('checked',true); 
    } else { 
      alert("n"); 
      $(this).nextAll('input[name="'+$(this).attr('name')+'Sub"]').prop('checked',false); 
    } 
    }); 
+0

$('body')做什么?这个甚至没有产生警报? – user1371896 2012-07-11 06:17:54