![](/img/trans.png)
[英]Table with Checkboxes - selected all Checkboxes and pass values to AJAX script
[英]Get selected checkboxes values from table and pass those values into ajax
我需要從表中獲取選中的復選框值,並將這些值傳遞給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,
},
您的代碼不干凈,因此我不確定這是您所需要的代碼,這是獲取multi復選框值的方法
<!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>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.