I am Trying to Insert the Selected Value of the checbox into an Array and display the array value.. However I got this error valueSelected.push is not a function
.. Can Anyone help me? Here's my code. Are there anything that I missed Out?
<html>
<head>
<script src="js/jquery-1.7.1.min.js"></script>
<script src="js/jquery-ui.js"></script>
<script src="js/jquery-ui.min.js"></script>
</head>
<body>
<table id="dataTbl">
<tr>
<td><input type='checkbox' id='chkVerify' name='chkVerify' value='Item1'></td>
<td>Item 1</td>
</tr>
<tr>
<td><input type='checkbox' id='chkVerify' name='chkVerify' value='Item2'></td>
<td>Item 2</td>
</tr>
<tr>
<td><input type='checkbox' id='chkVerify' name='chkVerify' value='Item3'></td>
<td>Item 3</td>
</tr>
</table>
<button type="button" class="deletebutton" name="delete_video" title="Show Selected Value" onclick="DelRow();">Show Selected Value</button>
<script>
function DelRow(){
var checkboxValArry=[];
var $chkbox_checked = $('tbody input[type="checkbox"]:checked', '#dataTbl');
if($chkbox_checked.length === 0){
alert("No Row Selected");
}
else{
var checkedBox = $('#chkVerify:checked');
for (var i=0; i<checkedBox.length; i++){
var valueSelected = $('#chkVerify:checked').val();
checkboxValArry = valueSelected.push();
alert(checkboxValArry);
}
}
}
</script>
</body>
</html>
答案 0 :(得分:1)
valueSelected
是一个字符串。
Push
仅适用于数组。
此外,你没有正确地推动元素。
试试这个
checkboxValArry.push(valueSelected);
console.log(checkboxValArry);
编辑
您的数据检索部分错误
试试这个
var checkboxValArry=[];
$('#chkVerify:checked').each(function(){
checkboxValArry.push($(this).val());
})
console.log(checkboxValArry);
答案 1 :(得分:1)
您可以简单地使用jQuery map
方法为您完成所有繁重的任务。
if($chkbox_checked.length === 0){
alert("No Row Selected");
}
else
{
checkboxValArry = $chkbox_checked.map(function(){
return this.value;
}).get();;
console.log(checkboxValArry);
}
Here是一个工作样本
另外,我注意到你有多个checckbox的id值相同。那是无效的。你应该保留唯一的id值。