2012-04-20 54 views
0

我有一個動態創建的html表。行和coloumns的數量西港島線是根據搜索結果中的每個搜索不同的。這是如何即時創建一個表獲取在複選框中選中元素的名稱

for(count = 0 ; count < facetList.length ;){ 
    facetsString+="<div id='test'>"; 
    facetsString+="<table id='facetTable' border='1' width='830px' style='margin-left:30px; margin-top:30px; font-family:Tahoma; '>"; 
    for(var i=0 ; i< 6 && count < facetList.length ; i++ ){ 
     facetsString+="<tr>"; 
     for(var j=0;j<4 && count < facetList.length ; j++){ 

      facetsString+="<td><form><input type='checkbox'></form>&nbsp;&nbsp;"+facetList[count].term+ "(" + facetList[count].count + ")" + "</td>"; 

      count++; 
     } 
    facetsString+="</tr>"; 
    } 
facetsString+="</table>"; 

facetsString+="</div>"; 
} 

return facetsString; 
}; 

現在有在table..I每個入口前的複選框要顯示的名稱檢查框的元素...

+1

'$( '輸入[類型=複選框]:選中')每個(函數(){ 的console.log($(本).attr( '姓名'));} )' – 2012-04-20 09:01:38

+0

你能解釋一下嗎? – Sam 2012-04-20 09:07:59

回答

1

這是使用的代碼。 。

function checkCount() 
{ 


var allVals = new Array(); 
    var newVals = new Array(); 

    $('#panel :input[type=checkbox]:checked').each(function() 
    { 
     $("#slctFacet").empty(); 
     newVals.push($(this).parent().text()); 
     allVals.push($(this).val()); 

    }); 
    //alert(allVals); 
    // alert(newVals); 

    var fctArrayLength=newVals.length; 
    //alert(fctArrayLength); 
    if(fctArrayLength==0) 
     { 
     $("#slctFacet").empty(); 
     } 
    else 
     { 
      for(var i=0;i<fctArrayLength;i++) 
       { 
      $("#slctFacet").append(newVals[i]); 


       } 
     } 
} 
$(":checkbox").click(checkCount); 
}; 
+0

這是ntwrking .. – Sam 2012-04-20 09:31:20

+0

您沒有定義複選框的值。請告訴您是否需要複選框的值或其下一個元素的值。順便說一句,我的代碼中有一個語法錯誤。我現在解決了。 – Chinmaya003 2012-04-20 09:46:56

+0

請告訴您是否需要像這樣的JSFiddle Link功能? http://jsfiddle.net/nmkSm/2/ – Chinmaya003 2012-04-20 10:03:46

0
// Get the form from its name 
var form = document.forms.formName, 
// Prepare an array to get the values 
    vals = [] 

// For each element in the form 
[].forEach.call(form.elements, function (el) { 

    // Check if it's a checkbox. If it is, check if it's checked 
    if (el.type === 'checkbox' && el.checked === true) { 

     // Add the value to the "vals" array 
     vals.push(el.value) 
    } 
}) 

// Now all the values are available in the "vals" array 
console.log(vals)