Javascript 如何获取复选框值并存储在数组中
在我的项目中,我想将复选框值插入数组 这是我的密码:Javascript 如何获取复选框值并存储在数组中,javascript,Javascript,在我的项目中,我想将复选框值插入数组 这是我的密码: $('#fpdId').click(function(){ var files = new Array(); //xzyId is table id. $('#xzyId tbody tr input:checkbox').each(function() { if (this.checked) { files.push(this.value); } }); co
$('#fpdId').click(function(){
var files = new Array();
//xzyId is table id.
$('#xzyId tbody tr input:checkbox').each(function() {
if (this.checked) {
files.push(this.value);
}
});
console.log(files);
});
<input type="button" id="fpdId" value="filePack">
$('#fpdId')。单击(函数(){
var files=新数组();
//xzyId是表id。
$('#xzyId tbody tr input:checkbox')。每个(函数(){
如果(选中此项){
files.push(这个.value);
}
});
console.log(文件);
});
我的html表格有三行,每行有三个tds
以下是表格代码:
<table border=1px class="xzy" id="xzyId" style="width:100%">
<colgroup>
<col style="width:10%">
<col style="width:80%">
<col style="width:10%">
</colgroup>
<tbody>
<tr>
<td ><input type="checkbox" value="x" >1</td>
<td >Stack</td>
<td >John</td>
</tr>
<tr>
<td ><input type="checkbox" value="y" >2</td>
<td >Stack</td>
<td >Sansa</td>
</tr>
<tr>
<td ><input type="checkbox" value="z" >3</td>
<td >Stack</td>
<td >Aya</td>
</tr>
</tbody>
</table>
1.
堆栈
约翰
2.
堆栈
桑萨
3.
堆栈
阿雅
但不幸的是,数组是空的,有什么问题吗?这将对您有所帮助 我变了
if ($(this).is(':checked')) {
}
$(“文档”).ready(函数(){
$('#fpdId')。单击(函数(){
var files=新数组();
//xzyId是表id。
$('#xzyId tbody tr input:checkbox')。每个(函数(){
如果($(this).is(':checked')){
files.push(这个.value);
}
});
console.log(文件);
});
})
1.
堆栈
约翰
2.
堆栈
桑萨
3.
堆栈
阿雅
这将对您有所帮助
我变了
if ($(this).is(':checked')) {
}
$(“文档”).ready(函数(){
$('#fpdId')。单击(函数(){
var files=新数组();
//xzyId是表id。
$('#xzyId tbody tr input:checkbox')。每个(函数(){
如果($(this).is(':checked')){
files.push(这个.value);
}
});
console.log(文件);
});
})
1.
堆栈
约翰
2.
堆栈
桑萨
3.
堆栈
阿雅
您是否可以在该表中添加html部分,包括表单和复选框?您是否已经尝试过搜索?在这里,您已经在jquery中给出了代码,但是标记是JavaScription。您可以将html部分添加到该表中,并带有表单和复选框吗?您已经尝试过搜索了吗?在这里,您已经在jquery中给出了代码,但是标记是Javascript这是jquery,标记是javascriptYah!您的@Omkaar.k是正确的,但上面问题中的代码内部使用的是jquery Right这是jquery,标记是javascriptYah!您的@Omkaar.k是正确的,但上述问题中的代码内部使用的是jquery