2012-06-15 132 views
0

我在表单中有一个表。它有两列,第一列是复选框,第二列是输入。发送具有相同ID的多个复选框的值

这里它的结构:

<table> 
<tr> 
<td> 
<input type="checkbox" name="choose" id="choose" class="choose"> 
</td> 
<td> 
<input type="text" name="item" id="item" class="item" > 
</td> 
</tr> 
</table> 

它是充满了从我的数据库信息,以便它可能有几行。

表单通过javascript函数提交给js文件,然后,由于jQuery的ajax,所有参数都转到我的控制器php文件中。

因为我想送我的PHP所有的值形成的文本输入,在我的js文件我做的:

 var arrayItem= []; 
     $(".item").each(function(){ 
      arrayItem.push($(this).val()); 
     }) 
    params += '&items='+ arrayItem; 
. 
. 
    //So I can do: 
     $.ajax ({ 
      url: myPHPUrl, 
      data: params, 
      type: "POST", 
      async:false, 
      success: function (data, textStatus) 
      { 
      } 
     }); 

现在我需要做相同的复选框,但我不知道如何进行。

任何人都可以帮助我吗?

非常感谢!

+1

相同的代码应与复选框工作。你只需要记住只发送选中的项目。即'$(“。choose:checked”)' – Imdad

+0

我同意@Imdad,而且不要忘记不要在页面中放置多个具有相同ID的控件(这是一个错误)。旁边的问题:为什么你的&items ='为你的数据? POST请求不需要像URL一样编码。 –

回答

0

你可以试试这个:

var arrayItem= []; 
    $(".choose:checked").each(function(index, elem){ 
     //you could store them like a key-valuepair so you no where the value belongs to. 
     //you can take the ID of the elem parm inside the for each and store it with the control id. 
     arrayItem.push(elem.val()); 
    }) 
params += '&items='+ arrayItem; 
+0

欢迎您,很高兴帮助您! –

相关问题