使用jQuery事件的表单提交的无限循环 [英] Infinite loop of form submit using jquery event
问题描述
当我尝试捕获表单提交时,代码陷入了循环.目的是在表单值消失之前对其进行替换.
The code is stuck in a loop when I try to catch a form submission. The purpose is to replace a value of the form before it goes out.
$('form').submit(function(e){
e.preventDefault();
$.ajax({
type: "POST",
url: 'convert.asp',
data: $('form').serialize(),
success: function(response){
$('input[name="field1"]').val(response);
$('form').submit();
}
});
return false;
});
有人有什么想法吗?
更新:我本来是将它绑定到按钮单击事件的,并且可以正常工作,但是我想保留提交按钮的[enter]键元素.看到代码有点不合逻辑,抓住按键会是一个更好的主意吗?
UPDATE: I originally had it bound to a button click event and it was working but I wanted to preserve the [enter] key element of the submit button. Seeing that the code is kind of illogical, would catching a keypress be a better idea?
推荐答案
我认为您的ajax试图在最终提交表单之前对其进行验证.在这种情况下,只需在提交表单之前取消验证功能的绑定即可.
I assume your ajax is an attempt to validate the form before its eventual submission. In that case just unbind the validation function before submitting the form.
success: function(response){
$('input[name="field1"]').val(response);
// Add unbind to remove validations
$('form').unbind().submit();
}
这篇关于使用jQuery事件的表单提交的无限循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!