表单提交时,Jquery函数BEFORE

Mat*_*ice 60 forms jquery form-submit

我试图在单击表单提交按钮时使用Jquery触发一个函数,但该函数需要在实际提交表单之前触发.

我试图在提交时将一些div标签属性复制到隐藏文本字段中,然后提交表单.

我已设法使用鼠标悬停功能(当提交按钮悬停时)使其工作,但这不适用于使用触摸的移动设备.

$("#create-card-process.design #submit").on("mouseover", function () {
    var textStyleCSS = $("#cover-text").attr('style');
    var textbackgroundCSS = $("#cover-text-wrapper").attr('style');
    $("#cover_text_css").val(textStyleCSS);
    $("#cover_text_background_css").val(textbackgroundCSS);
});
Run Code Online (Sandbox Code Playgroud)

我使用.submit函数,但是这些值不会保存在字段中,因为函数在提交表单时触发,而不是之前触发.

非常感谢

kar*_*uke 87

您可以使用onsubmit功能.

如果您返回false,表单将不会被提交.在这里阅读它.

$('#myform').submit(function() {
  // your code here
});
Run Code Online (Sandbox Code Playgroud)


Waq*_*ary 52

$('#myform').submit(function() {
  // your code here
})
Run Code Online (Sandbox Code Playgroud)

以上不适用于Firefox.表单只需提交而无需先运行代码.其他地方也提到了类似的问题......例如这个问题.工作将是

$('#myform').submit(function(event) {

 event.preventDefault(); //this will prevent the default submit

  // your code here (But not asynchronous code such as Ajax because it does not wait for response and move to next line.)

 $(this).unbind('submit').submit(); // continue the submit unbind preventDefault
})
Run Code Online (Sandbox Code Playgroud)

  • 我刚刚安装了 Firefox 来测试它。我使用的是 87 版本。简单的 `submit` 就可以了,不需要 `preventDefault`。我使用的是jquery 3.5.1。 (2认同)