jQuery表单提交与.on() [英] jquery form submit with .on()

查看:55
本文介绍了jQuery表单提交与.on()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试发送由jquery创建的表单. 表单被附加到div上,下面的变量"data"是使用php创建的,我将发布最重要的js代码.

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.

我尝试了很多有和没有'on()'的事情,但是我无法使警告框显示为'1',以至于我知道代码块实际上已经执行了.做错了,任何解释将不胜感激.提前致谢!

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!

$(".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);
});

推荐答案

您要在表单存在之前对其进行绑定.将事件绑定到文档,然后将表单选择器传递给.on:

You are binding to the form before it exists. Bind the event to the document, and pass the form selector to .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);
});

这篇关于jQuery表单提交与.on()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆