使用 .on() 提交 jquery 表单

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10554665/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 09:31:16  来源:igfitidea点击:

jquery form submit with .on()

jqueryforms

提问by JustaN00b

I'm trying to send a form created by jquery. The form is appended to a div and variable 'data' below is created using php, I'll just post the most important js code.

我正在尝试发送由 jquery 创建的表单。该表单被附加到一个 div 并且下面的变量“数据”是使用 php 创建的,我将发布最重要的 js 代码。

I tried many things with and without 'on()' but I fail in getting the alert box displaying the '1' so that I know the code block is actually executed..tbh I don't get what I'm doing wrong, any explanation would be greatly appreciated. Thanks in advance!

我尝试了很多有和没有 'on()' 的事情,但我没有得到显示 '1' 的警告框,所以我知道代码块实际上被执行了..tbh 我不明白我做错了什么,任何解释将不胜感激。提前致谢!

$(".r5").click(function(event){
    var data='<select name="sForum"><option value="1">bla</option></select>';
    $(this).parent().next('div').html('<form class="moveform">'+data+'<input type="submit" name="submit" class="movethisform" value="Move Thread" /></form>');
});
$("form.moveform").on("submit",function(event){
    alert(1);
});

回答by bfavaretto

You are binding to the form before it exists. Bind the event to the document, and pass the form selector to .on:

您在表单存在之前就绑定到表单。将事件绑定到文档,并将表单选择器传递给.on

$(".r5").click(function(event){
    var data='<select name="sForum"><option value="1">bla</option></select>';
    $(this).parent().next('div').html('<form class="moveform">'+data+'<input type="submit" name="submit" class="movethisform" value="Move Thread" /></form>');
});
$(document).on("submit", "form.moveform", function(event){
    alert(1);
});

回答by rt2800

have you tried

你有没有尝试过

$("inout.movethisform").on("click",function(event){
    alert(1);
    $("form.moveform").submit();
});