Sunday, February 13, 2011

JQuery Conditionally submit forms

I have a table with multiple rows. Each row is a form. I want to use JQuery to submit all the forms that have the check box checked. The form is posting to an IFrame so there is not much need for AJAX.

So far I have:

    $("form").submit();

which submits the form. but all forms. There is arbritary number of rows, could be 80-100.

  • This is kinda a guess since I'm not sure of your layout, but here goes....

    $("td > input:checked > form").submit();
    
    EndangeredMassa : If the selector returns null, you'll get an error.
    James Curran : The selector should never return null, only a collection with no items.
  • Off the top of my head, you probably want something like this

    $("input:checked").parent("form").submit();
    

    This will find the checked form fields, traverse up to find the parent form object and submit that.

    Jeremy B. : except if all rows are in the same form, it just submitted the whole form.
    Nathan Strutz : The poster said "Each row is a form" so my example still works.
  • I'm not sure about submit, because I've done this in .post(), but here is what I've used to get what I want to send on the submission:

    var formElements = $("form").find("input:checked").parent("td").serialize();
    

    you should be able to send back that variable.

    From Jeremy B.

0 comments:

Post a Comment