我需要从表中获取选中的复选框值,并将这些值传递给ajax。下面的代码中,我需要将selected_Device_Mac传递给ajax。我使用内部文本来获取单个mac值。我想检查多个复选框并将每个mac值传递给ajax。如何实现呢?
function getDeviceType() {
var selected_Device_Mac = '';
var selected_device_id = '';
($('#query-type').val() === 'single'){
if ($('#PA').is(":visible")) {
console.log("before")
selected_Device_Mac = document.getElementById("macaddr1").innerText;
selected_device_id = '#Selected_Device';
console.log("after")
} else if ($('#MP').is(":visible")) {
selected_Device_Mac = document.getElementById("macaddr2").value;
selected_device_id = '#Selected_Snmp_Device';
}
}
$('#cover-spin').show();
$.ajax({
method: "GET",
dataType: "text",
url: "getDeviceType",
data: {
mac : selected_Device_Mac,
},
答案 0 :(得分:1)
您的代码不干净,因此我不确定这是您所需要的 该代码是获取多复选框值的方法
<!DOCTYPE html>
<html>
<head>
<title>Title Page</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" integrity="sha384-1q8mTJOASx8j1Au+a5WDVnPi2lkFfwwEAa8hDDdjZlpLegxhjVME1fgjWPGmkzs7" crossorigin="anonymous">
</head>
<body>
<input type="checkbox" value="a">a
<input type="checkbox" value="b">b
<input type="checkbox" value="c">c
<input type="checkbox" value="d">d
<input type="checkbox" value="e">e
<button onclick="test()" class="btn btn-default">click</button>
<!-- jQuery -->
<script
src="https://code.jquery.com/jquery-1.12.4.min.js"
integrity="sha256-ZosEbRLbNQzLpnKIkEdrPv7lOy9C27hHQ+Xp8a4MxAQ="
crossorigin="anonymous"></script>
<!-- Bootstrap JavaScript -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js" integrity="sha384-0mSbJDEHialfmuBBQP6A4Qrprq5OVfW37PRR3j5ELqxss1yVqOtnepnHVP9aJ7xS" crossorigin="anonymous"></script>
<script>
var test = ()=>{
let check_list=[];
$('body').find("input[type=checkbox]:checked").each(function () {
check_list.push($(this).val());
});
alert(check_list);
}
</script>
</body>
</html>