我有一个动态创建的html表。根据搜索结果,每次搜索的行数和颜色数会有所不同。这就是我创建表格的方式
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> "+facetList[count].term+ "(" + facetList[count].count + ")" + "</td>";
count++;
}
facetsString+="</tr>";
}
facetsString+="</table>";
facetsString+="</div>";
}
return facetsString;
};
现在表格中的每个条目都有复选框..我想显示其复选框的元素名称......
答案 0 :(得分: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);
};
答案 1 :(得分: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 )