我在 div 标签内有一个表单,但使用 action=""
,并且我希望表单提交的结果出现在同一个 div 中,所以我创建了这个函数:
$(document).ready(function() {
$('#div1').delegate('form', 'submit', function() { // catch the form's submit events inside the div
$.ajax({ // create an AJAX call...
data: $(this).serialize(), // get the form data
type: $(this).attr('method'), // GET or POST
url: $(this).attr('action'), // the file to call
success: function(response) { // on success..
//$('#div1').html(response); // update the DIV
$('#div1').fadeOut('slow',function() {
$('#div1').html(response).fadeIn('slow');
});
}
});
return false; // cancel original event to prevent form submitting
});
});
但是,当我提交表单时,没有任何内容出现,因为表单没有要加载的操作,相同的功能适用于具有操作属性的其他表单,所以我的问题是如何处理这种情况以使表单读取脚本当表单操作为空时相同的页面?
最佳答案
首先,所有的表单都应该有一个 Action 。其 HTML 无效。
但是,如果我理解您想要发布到当前页面的问题,那么您可以执行此操作
url: $(this).attr('action') || window.location.href, // the file to call
这将采用操作属性(如果可用),或者如果操作不存在,则采用当前窗口位置的 href。
关于javascript - 加载表单,div 内不执行任何操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10575296/