0

I have multiple input fields like this:

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

I want to get them into jQuery and pass them with AJAX to php but remember the keys (3, 8, 12). Is this possible?

I tried until now the following:

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

2 回答 2

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 });
于 2013-04-20T19:57:45.977 回答
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();
于 2013-04-20T20:04:01.377 回答