2012-07-11 67 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