我有一个像这样的html html
<input type="checkbox" class="pushchecks" value="JAN" >
<input type="checkbox" class="pushchecks" value="FEB" >
<input type="checkbox" class="pushchecks" value="MAR" >
我需要获取选中的复选框值并以JSON格式传递到另一个页面
JQUERY
$('body').on('click','.pushchecks',function(){
var bills = {}; var i=0;
$(".checks:checked").each(function(){ bills = $(this).val(); i++; });
bills = JSON.stringify(bills);
$.load('process.php', { checkedbills:bills }, '.list',function(){});
});
流程.php
$billsAry = json_decode($_REQUEST['checkedbills']);
如果我选中 JAN 和 MAR 复选框,那么我需要在 process.php 页面中显示 JAN 和 MAR
最佳答案
只需对 Louys Patrice Bessette 代码进行一些编辑即可实现此功能。请注意,这里的请求是 POST request 。
test.html
<input type="checkbox" class="pushchecks" value="JAN" >
<input type="checkbox" class="pushchecks" value="FEB" >
<input type="checkbox" class="pushchecks" value="MAR" >
<!--to readable output from the php file-->
<pre class="result"><pre>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script>
$('body').on('click','.pushchecks',function(){
// An array.
var bills=[];
// Loop trought all .pushchecks checked.
// Edited to be .pushchecks instead of .checks
$(".pushchecks:checked").each(function(){
// Add this value in array.
bills.push( $(this).val() );
});
// Stingify the array. Would convert the array to json object
bills = JSON.stringify(bills);
// Ajax request to load process.php result in the pre tags above.
// Use $('*') to select all elements in the DOM
$('.result').load('process.php', { checkedbills:bills }, '.list',function(){});
});
</script>
process.php
<?php
$billsAry = json_decode($_REQUEST['checkedbills']);
print_r($billsAry); //The response and output to the request
?>
关于PHP JSON 数据传递,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44486071/