[英]how to pass values of several checkboxes with jquery ajax
To get the value from several checkboxes i use this code: 要从多个复选框中获取值,我使用以下代码:
<form class="myform" method="post" action="">
<input type="checkbox" class="checkbox" value="11" /><br>
<input type="checkbox" class="checkbox" value="22" /><br>
<input type="submit" value="Go" />
</form>
The ajax: ajax:
$(document).ready(function(){
$('.myform').on('submit', function(e){
//Stop the form from submitting itself to the server.
e.preventDefault();
var checkboxvalue = $('.checkbox').val();
$.ajax({
type: "POST",
url: '',
data: {checkboxvalue: checkboxvalue},
success: function(data){
$('.response').html(data);
}
});
});
});
The php: PHP:
if($_SERVER['REQUEST_METHOD'] == "POST") {
$value = false;
if(isset($_POST['checkboxvalue'])){
$value = $_POST['checkboxvalue'];
}
echo 'The value was: ' . $value;
exit;
}
The problem is: when checking the second checkbox, i get the value 11
, thats the value of the first checkbox. 问题是:当检查第二个复选框时,我得到值
11
,这是第一个复选框的值。 When clicking both checkboxes, i also get the value 11
单击两个复选框时,我也得到值
11
What i want to achieve: if i check the first checkbox, het should give me 11
as output. 我想要实现的目标:如果我检查第一个复选框,het应该给我
11
作为输出。 Checking the second checkbox, he should output 22
as value. 检查第二个复选框,他应输出
22
作为值。 And when checking both checkboxes, he should output 11
and 22
as value. 检查两个复选框时,他应输出
11
和22
作为值。
How can i achieve this? 我怎样才能实现这一目标?
USE THIS 用这个
$(document).ready(function(){
$('.myform').on('submit', function(e){
e.preventDefault();
var checkboxvalue = [];
$("input[type=checkbox]:checked").each(function(i){
checkboxvalue.push( i.value );
});
$.ajax({
type: "POST",
url: '',
data: {checkboxvalue: checkboxvalue},
success: function(data){
$('.response').html(data);
}
});
});
});
the php php
if($_SERVER['REQUEST_METHOD'] == "POST") {
$value = false;
/* the check box value in array*/
print_r($_POST['checkboxvalue']);
if(isset($_POST['checkboxvalue'])){
$value = $_POST['checkboxvalue'];
}
exit;
}
You need to added name attribute to check 您需要添加name属性才能检查
<input type="checkbox" class="checkbox" name="value-check" value="11" /><br>
var checkboxvalue = [];
$.each($("input[name='value-check']:checked"), function(){
checkboxvalue.push($(this).val());
});
You need to loop through
the checkbox which are selected, problem with your code is it is always giving you the value of first selected checkbox always 您需要
loop through
选中的复选框,代码问题是始终为您提供第一个选中复选框的值
var chkArray = []; $(".checkbox:checked").each(function() { chkArray.push($(this).val()); }); var selected; selected = chkArray.join(',') ; console.log(selected)
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.0.2/jquery.min.js"></script> <div><input type="checkbox" value="1" class="checkbox"> Value 1</div> <div><input type="checkbox" value="2" class="checkbox" checked="checked"> Value 2</div> <div><input type="checkbox" value="3" class="checkbox"> Value 3</div> <div><input type="checkbox" value="4" class="checkbox"checked="checked"> Value 4</div> <div><input type="checkbox" value="5" class="checkbox"> Value 5</div>
You need to create an array and fill with the values, if checkbox has been checked. 如果选中了复选框,则需要创建数组并填充值。
$(document).ready(function(){
$('.myform').on('submit', function(e){
//Stop the form from submitting itself to the server.
e.preventDefault();
var values = [];
$('.checkbox').each(function() {
if ($(this).is(':checked')) {
values.push($(this).val());
}
});
console.log(values);
});
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.