简体   繁体   English

当追加表单提交不起作用时

[英]when append form submit doesn't work

Here I created two forms: http://jsfiddle.net/sxGtM/4729/ when you submit a form ... data change to JSON 在这里,我创建了两种形式: http : //jsfiddle.net/sxGtM/4729/提交表单时...数据更改为JSON

$(document).ready(function(){
$("#test").append("<form action='' method='post'> <br>");
$("#test").append("test <input type='radio' name='test' value='test'> <br>");
$("#test").append("<input type='submit'> <br>");
$("#test").append("</form>");
});

The first is created in html and it works well, but the second is created in javascript with append and when you submit a form ... it is doing nothing 第一个是在html中创建的,并且效果很好,但是第二个是在javascript中使用append创建的,当您提交表单时...它什么也没做

Could you look at my code and write me a correct code? 您能看一下我的代码并给我写正确的代码吗?

Problem with you code is append is appending the form element as a whole ie, and the remaining elements are appended after form element. 您的代码问题是追加是将整个form元素追加,即,其余元素都追加到form元素之后。 your resulting html structure is something like this. 您得到的html结构是这样的。

<p id="test">
    <form action="" method="post"> <br>
    </form>
        test <input type="radio" name="test" value="test"> <br>
        <input type="submit"> <br>
</p>

So use .html() method to set the html content as a whole like below. 因此,请使用.html()方法将html内容设置为一个整体,如下所示。

$(document).ready(function(){
    $("#test").html("<form action='' method='post'> <br> test <input type='radio' name='test' value='test'> <br> <input type='submit'> <br></form>");
});

one more thing is that submit wont be bindable to the dynamically created elements. 还有一件事是,提交将不会绑定到动态创建的元素。 So use .on() delegates. 因此,请使用.on()委托。

Code: 码:

$('body').on('submit','form',function() {
        data = JSON.stringify($(this).serializeObject());
        $('#json').append(data);
        return false;
});

Fiddle 小提琴

you have a couple of things wrong... 你有几件事错了...

firstly, the html you append needs to be complete - by appending the opening and closing form tags separately the dom will consider it text. 首先,您添加的html需要完整-通过分别添加开始和结束表单标签,dom会将其视为文本。 you should probably consider appending the whole form at once or building it up as separate element. 您可能应该考虑一次附加整个表单,或者将其构建为单独的元素。

secondly, you are appending the form after you've bound the submit method - you will need to bind the submit method after the form has been added to the dom - again there are lots of ways you could go about doing this but a simple way is to have a function which you can call to bind your form. 其次,在绑定了commit方法之后,您将附加表单-将表单添加到dom后,您将需要绑定Submit方法-同样,您可以通过多种方法来完成此操作,但是这是一种简单的方法具有一个可以调用以绑定表格的函数。

see: 看到:

http://jsfiddle.net/w5anU/1/ http://jsfiddle.net/w5anU/1/

$.fn.serializeObject = function(){
    var o = {};
    var a = this.serializeArray();
    $.each(a, function() {
        if (o[this.name] !== undefined) {
            if (!o[this.name].push) {
                o[this.name] = [o[this.name]];
            }
            o[this.name].push(this.value || '');
        } else {
            o[this.name] = this.value || '';
        }
    });
    return o;
};

$(function() {
bindForms();
});

function bindForms() {
        $('form').submit(function() {
        data = JSON.stringify($('form').serializeObject());
        $('#json').append(data);
        return false;
    });
}

$(document).ready(function(){
$("#test").append("<form action='' method='post'> <br>test <input type='radio' name='test' value='test'> <br><input type='submit'> <br></form>");
bindForms();    
});

Thats because the following command : 那是因为以下命令:

$("#test").append("<form action='' method='post'> <br>");

also immediately adding a closing tag . 还立即添加一个结束标记。

you should create a tag, append it to #test, and then append all the rest elements to the element. 您应该创建一个标签,将其附加到#test,然后将所有其余元素附加到该元素。 (not to the #test element) (不适用于#test元素)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM