获取多个复选框的值并使用ajax使用其值执行操作

时间:2022-04-16 13:42:29

I have a table that has checkboxes, if a checkbox is selected i want a raw in the DB to be deleted - using ajax.

我有一个包含复选框的表,如果选中了复选框,我希望删除数据库中的原始数据 - 使用ajax。

with normal form i would simply name all the chexboxs some name lets say name="checkbox[]" and then simply use foreach($_POST['checkbox'] as $value){}

使用普通形式我只需将所有chexbox命名为name =“checkbox []”,然后只需使用foreach($ _ POST ['checkbox']作为$ value){}

now i am trying to get all the values of marked checkboxes then put them into an array. seems i am missing something though. here is what i go so far:

现在我试图获取标记复选框的所有值,然后将它们放入一个数组。似乎我错过了一些东西。这是我到目前为止:

var checkboxes = jQuery('input[type="checkbox"]').val();
var temp = new Array();
jQuery.each(checkboxes, function(key, value) { 
     temp[] = value;
});

Late on i will just pass temp as variable to ajax call.

我迟到只会将temp作为变量传递给ajax调用。

Is there anything i am missing ?

有什么我想念的吗?

3 个解决方案

#1


0  

You are trying to iterate over the Checkbox values which is wrong . Try this

您正在尝试迭代错误的Checkbox值。尝试这个

var $checkboxes = jQuery('input[type="checkbox"]') ;
var temp = new Array();
jQuery.each($checkboxes, function(i) {
     var value = $(this).attr('value'); 
     temp[i] = value;
});

If you want to pass only checked items just add a condition.

如果您只想传递选中的项目,只需添加一个条件。

if($(this).is(':checked')){
  var value = $(this).attr('value'); 
  temp[i] = value;
}

#2


3  

You can use :checked selector and map method:

您可以使用:checked selector和map方法:

var arr = $('input[type="checkbox"]:checked').map(function(){
     return this.value
}).get();

#3


0  

Just wrap the table in a form tag, and do this:

只需将表包装在表单标记中,然后执行以下操作:

var myData = jQuery('#myform').serialize();
jQuery.ajax('myscript.php', {data:myData});

#1


0  

You are trying to iterate over the Checkbox values which is wrong . Try this

您正在尝试迭代错误的Checkbox值。尝试这个

var $checkboxes = jQuery('input[type="checkbox"]') ;
var temp = new Array();
jQuery.each($checkboxes, function(i) {
     var value = $(this).attr('value'); 
     temp[i] = value;
});

If you want to pass only checked items just add a condition.

如果您只想传递选中的项目,只需添加一个条件。

if($(this).is(':checked')){
  var value = $(this).attr('value'); 
  temp[i] = value;
}

#2


3  

You can use :checked selector and map method:

您可以使用:checked selector和map方法:

var arr = $('input[type="checkbox"]:checked').map(function(){
     return this.value
}).get();

#3


0  

Just wrap the table in a form tag, and do this:

只需将表包装在表单标记中,然后执行以下操作:

var myData = jQuery('#myform').serialize();
jQuery.ajax('myscript.php', {data:myData});