Go Back   CodingForums.com > :: Client side development > JavaScript programming > Ajax and Design

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 01-23-2012, 04:09 AM   PM User | #1
q1h
New Coder

 
Join Date: May 2011
Posts: 34
Thanks: 7
Thanked 0 Times in 0 Posts
q1h is an unknown quantity at this point
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 ...
q1h is offline   Reply With Quote
Old 01-23-2012, 07:12 AM   PM User | #2
devnull69
Senior Coder

 
Join Date: Dec 2010
Posts: 2,260
Thanks: 10
Thanked 532 Times in 526 Posts
devnull69 will become famous soon enough
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!
devnull69 is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 08:05 AM.


Advertisement
Log in to turn off these ads.