foreach($data as $row)
{
$output .= '
<tr>
<td class="col-xs-3">'.$row->policyno.'</td>
<td class="col-xs-3">'.$row->q_tree.'</td>
<td class="col-xs-3">'.$row->Tt_tree.'</td>
<td class="col-xs-3">
<input id="checkbox" style="margin-left:20px" value='.$row->policyno.' type="checkbox" name="plcdata[]" />
</td>
</tr>
';
}
我使用这个循环来查看表中的数据,并使用Ajax传递到查看页面,并且添加了一个复选框作为表中选择行的输入。我的问题是如何使用javascriptname="plcdata[]
数组的复选框输入来获取选定的行?
我将获得所有选中的行,并将它们存储在一个数组中:
let values = [];
$("#checkbox").each(function() {
if($(this).is(":checked")){
values.push($(this).parent().parent()); //first parent is td, it parent is tr
}
});
您可以使用以下方法获取所有选中的复选框:
<input id="UNIQUE_ID" style="margin-left:20px" value='.$row->policyno.' type="checkbox" name="plcdata[]" class="plcdata"/>
var policyno = [];
var checkedBoxes = $('.plcdata:checked');
$.each(checkedBoxes, function () {
policyno.push(value);
})
你还应该使用唯一的id为所有复选框,如果你想添加id,你可以删除它
<div><input type="checkbox" value="4" class="chk"> Value 4</div>
值5
$(document).ready(function(){
$('#getValue').on('click', function(){
// Declare a checkbox array
var chkArray = [];
// Look for all checkboxes that have a specific class and was checked
$(".chk:checked").each(function() {
chkArray.push($(this).val());
});
// Join the array separated by the comma
var selected;
selected = chkArray.join(',') ;
// Check if there are selected checkboxes
if(selected.length > 0){
alert("Selected checkboxes value: " + selected);
}else{
alert("Please select at least one checkbox.");
}
});
});
this was found on another web page. this is work properly. that was solved my problem.