2012-06-15 49 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

歡迎您,很高興幫助您! –

相關問題