Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    q1h
    q1h is offline
    New Coder
    Join Date
    May 2011
    Posts
    34
    Thanks
    7
    Thanked 0 Times in 0 Posts

    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 ...

  • #2
    Senior Coder
    Join Date
    Dec 2010
    Posts
    2,396
    Thanks
    12
    Thanked 569 Times in 562 Posts
    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!


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •