简体   繁体   中英

How to submit a dynamically created form using JQuery?

Submit is not working form me, please can anyone can solve this ?

$(function(){
    $('#postImg').click(function() {

        var form = $("<form/>", {
            action: "checkout.php",
            method: "POST"
        });

        form.on("submit");
        $.each(['tprice', 'tevents'], function(i, v) {
            $("<input/>", {
                type: "hidden",
                name: v,
                value: $("#" + v).text()
            }).appendTo(form);
        });
        form.submit();
        return false;
    });
});

What you are doing here is trying to make an HTTP POST request in a really roundabout way (creating a form just for the purpose, populating it, posting, and then discarding).

It would be much easier and less confusing to directly use jQuery.post instead.

For example:

var parameters = {
    'tprice': $("#tprice").text(),
    'tevents': $("#tevents").text()
};
$.post("checkout.php", parameters);

Why are you trying to post a form that's not being bound into the DOM ? Maybe

$.post("checkout.php", { paramName: "value", paramName2: "value2" } );

is what you need ?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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