Gra*_*ter 8 php ajax checkbox jquery
我不知道如何传递复选框的选定值.任何帮助或建议对我都有很大帮助.
截至目前,这是我的代码,我被困在传递复选框的值
index.php<table>
<?php
foreach($response as $item){
echo '<tr><td><input type="checkbox" value="' .$item['id']. '"></td><td>' . $item['name'] . '</td></tr>';
}
?>
</table>
<button type="button" class="btnadd">AddSelected</button>
<script type="text/javascript">
$(function() {
$('.btnadd').click(function() {
$.ajax({
url: 'process.php',
type: 'post',
data: { }, // what should I put here to pass the value of checked checkboxes
success: function(data) {}
});
});
});
</script>
Run Code Online (Sandbox Code Playgroud)
process.php<?php
$array_ids = $_POST['ids']; // this will retrieve the id's
?>
Run Code Online (Sandbox Code Playgroud)
Dip*_*mar 20
试试这个.
HTML代码
<script type="text/javascript" src="js/jquery.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$('.btnadd').click(function(){
var checkValues = $('input[name=checkboxlist]:checked').map(function()
{
return $(this).val();
}).get();
$.ajax({
url: 'loadmore.php',
type: 'post',
data: { ids: checkValues },
success:function(data){
}
});
});
});
</script>
<input type="checkbox" name="checkboxlist" value="1" checked="checked" />
<input type="checkbox" name="checkboxlist" value="2" checked="checked" />
<input type="checkbox" name="checkboxlist" value="4" />
<input type="checkbox" name="checkboxlist" value="5" checked="checked" />
<input type="checkbox" name="checkboxlist" value="6" />?Run Code Online (Sandbox Code Playgroud)
loadmore.php代码
<?php
print_r($_POST['ids']);
?>Run Code Online (Sandbox Code Playgroud)
在loadmore.php中输出
Array
(
[0] => 1
[1] => 2
[2] => 5
)Run Code Online (Sandbox Code Playgroud)
而已.
干杯.
使用此功能:
function checkboxValues() {
var allVals = [];
$(':checkbox').each(function() {
allVals.push($(this).val());
});
return allVals; // process the array as you wish in the function so it returns what you need serverside
}
Run Code Online (Sandbox Code Playgroud)
你的ajax调用将如下所示:
$.ajax({
url: 'process.php',
type: 'post',
data: { checkboxValues() },
success:function(data){ }
Run Code Online (Sandbox Code Playgroud)