如果存在event.preventDefault函数,则ajaxForm不起作用

ajaxForm not working if there is a event.preventDefault function

本文关键字:ajaxForm 不起作用 函数 存在 event preventDefault 如果      更新时间:2024-05-06

大家好,我正在尝试使用jQuery form插件提交一个带有文件的表单,并且我正在使用HTML5属性验证表单,这就是为什么我添加了一个"event.prventDefault();"

问题是,它不工作,也没有显示任何错误消息。

这是代码

function showResponse(responseText){
    alert(responseText);
}
function beforeSub(){
    alert("called");
}
$('#prodFormBtn').click(function(){
$("#addProductForm").ajaxForm({
    beforeSubmit: beforeSub,
    success: showResponse
}).submit(function(e){
    e.preventDefault();
});

尝试:

$(document).ready(function () {
  function showResponse(responseText){
     alert(responseText);
  }
  function beforeSub(){
    alert("called");
  }
  $("#addProductForm").ajaxForm(showResponse);
});

函数ajaxForm通过ajax通过表单进行交互,只需设置成功事件处理程序(showResponse)即可。

如果您使用HTML5验证,那么该验证是浏览器在表单提交时所做工作的一部分。如果您希望启动HTML5验证,请不要在表单提交时preventDefault()