CodingForums.com

CodingForums.com (http://www.codingforums.com/index.php)
-   Ajax and Design (http://www.codingforums.com/forumdisplay.php?f=55)
-   -   Simplify jQuery syntax for form submission (http://www.codingforums.com/showthread.php?t=249548)

q1h 01-23-2012 04:09 AM

Simplify jQuery syntax for form submission
 
I'm using this code to submit a hidden form from a link:

Code:

    $("#request").submit(function () {
        $.ajax({
            type: "POST",
            cache: false,
            url: "forms/add.php",
            data: $(this).serialize(),
            success: function (data) {
                $('#result').html(data);
            }
        });
        return false;
    });
   
    $("#request-link").live("click", function () {
        $("#request").submit();
    });

Is there anyway to combine it into one command instead of having the seperate statements? Thanks ...

devnull69 01-23-2012 07:12 AM

This would ONLY make sense if you ONLY want to submit the form by clicking on #request-link. In that case you can combine it to
Code:

$("#request-link").live("click", function () {
    $("#request").submit(function() {
        $.ajax({
            type: "POST",
            cache: false,
            url: "forms/add.php",
            data: $(this).serialize(),
            success: function (data) {
                $('#result').html(data);
            }
        });
        return false;
    }).submit();
});

This will bind the .submit() handler ONLY when you click on the #request-link!


All times are GMT +1. The time now is 09:02 PM.

Powered by vBulletin®
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.