如何使用jQuery获取所有选中复选框的值

我不知道如何传递复选框的选定值。 任何帮助或建议对我都有很大帮助。

截至目前,这是我的代码,我被困在传递复选框的值

index.php

  <?php foreach($response as $item){ echo ''; } ?> 
' . $item['name'] . '
$(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) {} }); }); });

process.php

  

试试这个。

HTML代码

        

loadmore.php代码

  

在loadmore.php中输出

 Array ( [0] => 1 [1] => 2 [2] => 5 ) 

而已。

干杯。

使用此function:

  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 } 

你的ajax调用将如下所示:

 $.ajax({ url: 'process.php', type: 'post', data: { checkboxValues() }, success:function(data){ } 

你可以在你的ajax调用之前使用这样的东西:

 var ids=[]; $('input[type=checkbox]:checked').each(function(){ ids.push($(this).val()); }); 

但强烈建议将复选框包装在某个div中,并在jquery选择器中表示它。 因为在当前方式中,您可以选择并向服务器发送页面上的其他一些复选框。

将您的复选框包装在

标签中,并使用PHP数组表示法命名您的复选框:

 

使用jQuery.serialize()

  $(function () { $('.btnadd').click(function () { $.ajax({ url: 'process.php', type: 'post', data: $("#form1").serialize(), success: function (data) {} }); }); }); 

在服务器端, $_POST["item"]将是一个仅包含已检查值的数组。

获取复选框列表标签文本

       var chkbox = document.getElementsByName('mycheckbox[id][]'); var vals = ""; for (var i=0, n=chkbox .length;i 

希望它对你有用

请尝试这样的HTML:

 " ?> 

ajax / jquery:

 $(document).ready(function(){ //handle test button click $("button#importSikep").click(function(){ var checkValues = $('input[name=chkimportsikep]:checked').map(function() { return $(this).val(); }).get(); alert(checkValues); $('#loading-indicator-runsikep').show(); $.ajax({ type:"POST", url:"runsikepimport.php", // your php page action data:{ chkimportsikep: checkValues }, // cache:false, success: function(result){ $('#loading-indicator-runsikep').hide(); alert(result); $('.modal.in').modal('hide'); $("#description-status-sikep").load(location.href + " #description-status-sikep"); //location.reload(true); } }); return false; }); 

});

PHP动作:

  // don't need array variable, but your using $_POST if(!empty($_POST['chkimportsikep'])) { foreach($_POST['chkimportsikep'] as $table_name) { // do something } }