Php 如何使用ajax回调来确定何时提交表单
我想在用户必须在我提交表单之前单击提交按钮之后,使用javascript修改表单。该操作涉及一个ajax调用,必须在我提交表单之前完成。问题在于,提交事件处理程序将在ajax完成之前完成执行并返回false,而我还无法确定如何在ajax完成后提交表单。虽然我知道还有其他方法可以做到这一点,但我想知道是否有办法做到这一点。谢谢Php 如何使用ajax回调来确定何时提交表单,php,callback,onsubmit,Php,Callback,Onsubmit,我想在用户必须在我提交表单之前单击提交按钮之后,使用javascript修改表单。该操作涉及一个ajax调用,必须在我提交表单之前完成。问题在于,提交事件处理程序将在ajax完成之前完成执行并返回false,而我还无法确定如何在ajax完成后提交表单。虽然我知道还有其他方法可以做到这一点,但我想知道是否有办法做到这一点。谢谢 $("form#profile").submit(function() { var dept; if (IsEmpty($("input[name=dept
$("form#profile").submit(function() {
var dept;
if (IsEmpty($("input[name=dept_id1]").val())) {
return true;
} else {
dept = $("input[name=dept_id1]").val();
$.post("funcs.php", {dept:dept}, function(d) {
$("select[name=dept_id]").val(d);
// It is at this point that I want to submit the form
return true;
});
}
return false;
});
您要做的是在jQueryAjax调用中使用async:false。您不能使用$.post执行此操作,但实际上需要使用$.ajax。大概是这样的:
var d = $.ajax({
type: 'POST',
async: false,
url: "funcs.php",
data: "dept="+dept,
}).responseText;
$("select[name=dept_id]").val(d);
return true;
还要注意的是,您应该使用us$profile来代替$formprofile,因为它是一个更快的选择器,并且由于id应该是唯一的,所以它将选择相同的元素。您可以使用jquery event.preventDefault来实现这一点 这将阻止事件被触发。然后,你可以做所有你需要的事情,最后用$.post继续文章 下面是一个来自jquery网站的示例:
/* attach a submit handler to the form */
$("#searchForm").submit(function(event) {
/* stop form from submitting normally */
event.preventDefault();
/* get some values from elements on the page: */
var $form = $( this ),
term = $form.find( 'input[name="s"]' ).val(),
url = $form.attr( 'action' );
/* Send the data using post and put the results in a div */
$.post( url, { s: term },
function( data ) {
var content = $( data ).find( '#content' );
$( "#result" ).empty().append( content );
}
);
});
阅读此处的更多信息:感谢您的回复,只有在我单击提交按钮两次后,表单才会提交。还有什么建议吗?在表单提交之前,我必须点击提交按钮两次。
/* attach a submit handler to the form */
$("#searchForm").submit(function(event) {
/* stop form from submitting normally */
event.preventDefault();
/* get some values from elements on the page: */
var $form = $( this ),
term = $form.find( 'input[name="s"]' ).val(),
url = $form.attr( 'action' );
/* Send the data using post and put the results in a div */
$.post( url, { s: term },
function( data ) {
var content = $( data ).find( '#content' );
$( "#result" ).empty().append( content );
}
);
});