答案 0 :(得分:1)
在客户端:
<input name = "destination_1" type = "checkbox">
<input name = "destination_2" type = "checkbox">
....
在服务器上:
<?php
$values = [];
for($i = 1; $i < $maxCheckBoxCount; $i++) {
if(isset($_POST['destination_'.$i])) {
$values[] = $i;
}
}
?>
那么$值就是你的答案;
答案 1 :(得分:1)
您应该将checkbox
名称设为这样的数组。
<input class="destination" name = "destination[]" type = "checkbox" value="1"> Name1
<input class="destination" name = "destination[]" type = "checkbox" value="2"> Name2
<input class="destination" name = "destination[]" type = "checkbox" value="3"> Name3
<input class="destination" name = "destination[]" type = "checkbox" value="4"> Name4
<button id="abc">ALert Values</button>
在php中,您可以将复选框中的选定值作为此类
的数组Array
(
[destination] => Array
(
[0] => 1
[1] => 2
[2] => 3
)
)
仅考虑用户选择name1, name2 and name3
你的javascript
<script>
$("#abc").click(function (event) {
event.preventDefault();
var searchIDs = $(".destination:checkbox:checked").map(function () {
return $(this).val();
}).get(); // <----
alert(searchIDs);
});
</script>
它将提醒所有选中的复选框。
答案 2 :(得分:0)
以下是您正在运行的代码示例。
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script>
function getVal()
{
var selected = new Array();
$("input:checkbox[name=destination]:checked").each(function() {
selected.push($(this).val());
});
}
</script>
</head>
<body>
<input name = "destination" type = "checkbox" value="1"> Name1
<input name = "destination" type = "checkbox" value="2"> Name2
<input name = "destination" type = "checkbox" value="3"> Name3
<input name = "destination" type = "checkbox" value="4"> Name4
<input type="button" name="btnProcess" value="Process" onclick="getVal()">
</body>
</html>
您可以根据需要进行修改。