【发布时间】:2017-06-29 21:09:05
【问题描述】:
我真的不知道如何使用 Ajax 发布数据功能,但我正在使用一些免费的简单文件并尝试对其进行一些调整。
我要更改的简单代码部分是:
post_data = {
'clcon_user_name' : $('input[name=name]', clientConParent).val(),
'clcon_user_lastname' : $('input[name=lastname]', clientConParent).val(),
'clcon_user_email' : $('input[name=email]', clientConParent).val(),
'clcon_phone_number' : $('input[name=phone]', clientConParent).val(),
'clcon_msg' : $('textarea[name=message]', clientConParent).val(),
'pro_mailto' : $('input[name=promailto]', clientConParent).val()
};
我想让它动态化,这样它就会循环遍历我表单上的字段并以这种格式获取它们:
'npf_{字段名称atrr}' : $('input[name={字段名称atrr}]', theForm).val(),
用于 post_data(用于 Ajax $.post(... 函数)。
我试过了:
post_data = {
$("input, textarea, select", theForm).each(function(){
'\'npf_$'+$(this).attr('name')'\'' : $('input[name='++$(this).attr('name')+']', theForm).val(),
});
};
没用,也试过了:
post_data = {
$("input, textarea, select", theForm).each(function(){
var theName = $(this).attr('name');
'npf+theName' : $('input[name='+theName+']', theForm).val(),
});
};
没用,也试过了:
post_data = {
$("input, textarea, select", theForm).each(function(){
var theName = $(this).attr('name');
'npf+theName' : $('input[name='+theName+']', theForm).val(),
});
};
我知道我一定做错了几件事...我可以使用一些“傻瓜”的解释感觉我是初学者。
【问题讨论】:
-
使用
serializeArray然后简单地使用change the names of the properties 来匹配你想要的。
标签: jquery ajax loops variables