2013-04-20 126 views
0

我有多个输入字段这样的:获取jQuery中输入数组的值,并将其传递给PHP

<input type="text" name="childage[3]" placeholder="0"/> 
<input type="text" name="childage[8]" placeholder="0"/> 
<input type="text" name="childage[12]" placeholder="0"/> 

我想他们到jQuery和与AJAX它们传递到PHP但要记住的钥匙(3 ,8,12)。这可能吗?

我想直到现在以下几点:

$('input[name="childage[]"]'); 
$('input[name="childage"'); 

回答

3

你应该看看serialize方法:

http://docs.jquery.com/Ajax/serialize

你会想抓住所有的表单元素,像这个:

<input class="age-box" type="text" name="childage[3]" placeholder="0"/> 
<input class="age-box" type="text" name="childage[8]" placeholder="0"/> 
<input class="age-box" type="text" name="childage[12]" placeholder="0"/> 

var ages = $('.age-box').serialize(); 
$.post(url, ages, function(data) { //handle response from the server }); 
+0

请注意,serialize将替换用于url编码的'['和']'。你将不得不将它们替换回'$('。age-box')。serialize()。replace(/%5B/g,'[').replace(/%5D/g,']');'或与类似的东西。 – Spokey 2013-04-20 20:07:09

+0

谢谢!在PHP中,我使用了parse_str($ _ POST ['fields'],$ fields);它的工作原理 – 2013-04-20 20:08:37

0

你可以使用隐藏输入字段并将值设置为您需要的数字:

<input type="hidden" value=3 /> 
<input type="hidden" value=8 /> 
<input type="hidden" value=12 /> 

// Get the values 
$('input[type="hidden"]')[0].val(); 
$('input[type="hidden"]')[1].val(); 
$('input[type="hidden"]')[2].val(); 
相关问题