在jquery中获取输入数组的值并将其传递给PHP
我有多个输入字段,如下所示:在jquery中获取输入数组的值并将其传递给PHP,jquery,html,ajax,Jquery,Html,Ajax,我有多个输入字段,如下所示: <input type="text" name="childage[3]" placeholder="0"/> <input type="text" name="childage[8]" placeholder="0"/> <input type="text" name="childage[12]" placeholder="0"/> <input class="age-box" type="text" name="chi
<input type="text" name="childage[3]" placeholder="0"/>
<input type="text" name="childage[8]" placeholder="0"/>
<input type="text" name="childage[12]" placeholder="0"/>
<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 });
您应该看看
序列化方法:
您需要获取所有表单元素,如下所示:
<input type="text" name="childage[3]" placeholder="0"/>
<input type="text" name="childage[8]" placeholder="0"/>
<input type="text" name="childage[12]" placeholder="0"/>
<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 });
var ages=$('.age-box').serialize();
$.post(url、年龄、函数(数据){//处理来自服务器的响应});
您应该看看序列化方法:
您需要获取所有表单元素,如下所示:
<input type="text" name="childage[3]" placeholder="0"/>
<input type="text" name="childage[8]" placeholder="0"/>
<input type="text" name="childage[12]" placeholder="0"/>
<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 });
var ages=$('.age-box').serialize();
$.post(url、年龄、函数(数据){//处理来自服务器的响应});
您可以使用隐藏的输入字段,并将值设置为所需的数字:
<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();
//获取值
$('input[type=“hidden”]”)[0].val();
$('input[type=“hidden”]”)[1].val();
$('input[type=“hidden”]')[2].val();
您可以使用隐藏的输入字段,并将值设置为所需的数字:
<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();
//获取值
$('input[type=“hidden”]”)[0].val();
$('input[type=“hidden”]”)[1].val();
$('input[type=“hidden”]')[2].val();
注意,serialize将取代url编码的[
和]
。您必须将它们替换回$(“.age box”).serialize().replace(/%5B/g,[').replace(/%5D/g,['))代码>或类似的。谢谢!在php中,我使用了parse_str($_POST['fields'],$fields);请注意,serialize将取代url编码的[
和]
。您必须将它们替换回$(“.age box”).serialize().replace(/%5B/g,[').replace(/%5D/g,['))代码>或类似的。谢谢!在php中,我使用了parse_str($_POST['fields'],$fields);它是有效的