开发者

Don't touch jQuery when you add new form

Let's say I have an input: <input type="text" id="smth" value="" /> and I have jQuery to pass this data to .php file

$("#submit_button").click(functi开发者_运维知识库on() {
        var info = {};
        info['smth'] = $('#smth').val();
        $.ajax({
            type: "POST",
            url: "insert.php",
            data: info,
            success: function(msg){
                alert("Well done!");
            }
        })
})

It works great, but let's say I need to add new input, let's say:

<input type="text" id="smth2" value="" />,

so I need in jQuery add this line after info['smth']:

info['smth2'] = $('#smth2').val();.

My question is, how can I avoid this? Is it possible to not add new line in jQuery every time I add new input? How would it look like? Thanks.


Hopefully, you're adding all your <input>s to a <form>. Hopefully, all those <input>s have name attributes (eg your first one should have name="smth"). In this case, the problem is solved for you!

var info = $('#myForm').serialize();

Done!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜