[英]How to pass form element values to the ajax success function?
I'm submitting a number of dynamic forms via ajax using this code: 我使用以下代码通过ajax提交了许多动态表单:
$(document).on('submit', 'form', function (e) {
$.ajax({
type: 'post',
url: 'data/process.php',
data: $(this).serialize(), // <----"this" is current form context
success: function () {
gaddress();
},
error: function (xhr) { //<----error function
gerroraddress(xhr);
}
});
e.preventDefault();
});
The code works well but the issue is that I want to be able to provide dynamic response like below: 该代码运行良好,但问题是我希望能够提供如下所示的动态响应:
function gaddress(form) {
if (form = "addressform") {
var msg = "Address form updated successfully.";
jQuery.jGrowl(msg, {
life: 10000,
position: 'center'
});
vaddress();
}
}
That way, I will provide appropriate response and redirect after successful form submit based on the form submitted. 这样,在成功提交表单后,我将根据提交的表单提供适当的响应并重定向。
How do I get the form name being submitted or the name of one of the form elements so I can pass it as a parameter to my success function: gaddress()
? 如何获取要提交的表单名称或表单元素之一的名称,以便将其作为参数传递给成功函数gaddress()
?
Also, I need to show a loading bar when form is processing and exit the loading bar after successful completion. 此外,我需要在处理表单时显示加载栏,并在成功完成后退出加载栏。
You can do: 你可以做:
$(document).on('submit', 'form', function (e) {
var form = $(this).attr("id"); //Or any attribute here
$.ajax({
type: 'post',
url: 'data/process.php',
data: $(this).serialize(), // <----"this" is current form context
success: function () {
gaddress(form);
},
error: function (xhr) { //<----error function
gerroraddress(xhr);
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.