0

I have multiple forms on this page I am optionally submitting with ajax. Problem is only the first object select by jQuery is being executed!

Here's the js:

$(function() {
var input = 'table td input[type="checkbox"]';
var form = 'form.update_done';
var isDone = 0;

$(input).each(function() {
    if($(this).attr('checked')) { $(this).parents('tr').removeClass('unDone'); }
    else { $(this).parents('tr').addClass('unDone'); }
});

$(input).each(function() {
    $(this).click(function update() {
        if($(this).attr('checked')) {
            isDone = 1;
            $(form).submit();
            $(this).parents('tr').removeClass('unDone');
        }
        else {
            isDone = 0;
            $(form).submit();
            $(this).parents('tr').addClass('unDone');
        }
    });
});

$(form).submit(function() {
    $.post(
        'set_done.cfm',
        { id: $('input[name="id"]').val(), done: isDone },
        function(responseText){  
            // $(this).parents('tr').toggleClass('unDone'); 
        },  
        "html"
    );
    return false;
});
});

and the html:

<td>
<form class="update_done">
<input type="hidden" name="id" value="#jobs.id#" />
<input type="checkbox" <cfif jobs.done IS 1>checked="checked" </cfif>/>
</form>
</td>

Anyone know where I went off track? If I wasn't very clear, please let me know.

IzzyCooper
  • 586
  • 1
  • 5
  • 14
  • Note: the html code is in a tag, and is in multiples on the page. – IzzyCooper Apr 28 '11 at 03:23
  • Have you checked to make sure your output HTML is valid? – mattsven Apr 28 '11 at 03:30
  • Not 100%. But that shouldn't really interfere w/ jquery. The document is using xhtml transitional. I know, forms don't belong in tables. – IzzyCooper Apr 28 '11 at 03:31
  • I meant so that you can be sure one of your input tags isn't broken. – mattsven Apr 28 '11 at 03:33
  • @Yisroel - are you saying that only the first form is being submitted? Or that all your forms submit properly, but with only the first input in that form going through? – Chris Rogers Apr 28 '11 at 03:33
  • @Chris - Only the first form submits. When querying sql for rows where column done = 0, it only returns the first forms id. – IzzyCooper Apr 28 '11 at 03:38
  • @NeXXeuS - I double checked my code. It's all fine!
    – IzzyCooper Apr 28 '11 at 03:39
  • @Yisroel, it would appear from your code that you are updating ALL records to status of the last updated row. So if you checked off Done on 10 records, and then unchecked one record, all 10 records would be updated as not done. – Yisroel Apr 28 '11 at 14:36

4 Answers4

1

Are you saying that you're trying to submit multiple forms on the same click event? If so, I believe the problem is that your return false; in your submit() (in order to cancel the form submit, right?). However, with jQuery, return false; is equivalent to calling event.stopPropagation() and event.preventDefault(). In your case, you do not want to stop propagation of this event since it stops your other forms from being submitted.

I believe changing submit handler to the following should fix your problem:

$(form).submit(function(e) {
    $.post(
        'set_done.cfm',
        { id: $('input[name="id"]').val(), done: isDone },
        function(responseText){  
            // $(this).parents('tr').toggleClass('unDone'); 
        },  
        "html"
    );
    e.preventDefault();
});

Karim explains:

return false from within a jQuery event handler is effectively the same as calling both e.preventDefault and e.stopPropagation on the passed jQuery.Event object.

e.preventDefault() will prevent the default event from occuring, e.stopPropagation() will prevent the event from bubbling up and return false will do both. Note that this behaviour differs from normal (non-jQuery) event handlers, in which, notably, return false does not stop the event from bubbling up.

Community
  • 1
  • 1
no.good.at.coding
  • 20,221
  • 2
  • 60
  • 51
  • Problem might be since I didn't call .each() form. Therefore jQuery might only be submitting the first form. Any suggestions? – IzzyCooper Apr 28 '11 at 03:46
  • Well, you don't need the `.each()` at all for attaching an event handler (see http://jsfiddle.net/nogoodatcoding/tSaYa/). Are you sure you don't mean `$('form').submit();` instead of `$(form).submit();`? The first trigger the submit event on all form while your code will trigger the submit *only* on `form.update_done` which is what the variable is set to. – no.good.at.coding Apr 28 '11 at 04:59
  • Also, see http://jsfiddle.net/nogoodatcoding/jfDBL/ - I have two forms and I trigger the submit on all forms when either of the two checkboxes is checked/unchecked. I use `preventDefault()` to stop the default action of the form submission from happening, but do not stop the bubbling of the event, thus allowing the next form submit to also be triggered and then preventing that also. – no.good.at.coding Apr 28 '11 at 05:04
  • Finally found it! $.post( 'set_done.cfm', { id: $(this).children('input[type="hidden"]').val(), done: isDone }, function(responseText){ // $(this).parents('tr').toggleClass('unDone'); }, "html" ); My mistake was that I originally set the "POST id" to the first input found! So much for small mistakes in big code! Thanks everyone for helping. If not for @no.good.at.coding I would still be with no answer. – IzzyCooper Apr 28 '11 at 05:18
  • Yisroel, perhaps you can submit your own answer so people know the issue is resolved. – Yisroel Apr 28 '11 at 14:34
  • 1
    Glad you found a fix :) I agree with Yisroel, you could submit your own answer and select it as the solution so that this question can be closed. – no.good.at.coding Apr 28 '11 at 14:40
  • @no.good.at.coding thanks! I needed to wait 8 hours to post my answer according to the rules on the site. – IzzyCooper Apr 28 '11 at 17:52
1

Finally found it!

$(form).submit(function(e) {
    e.preventDefault();
    $.post(
        'set_done.cfm',
        { id: $(this).children('input[type="hidden"]').val(), done: isDone },
        function(responseText){  
            // $(this).parents('tr').toggleClass('unDone'); 
        },  
        "html"
    );
});

My mistake was that I originally set the "POST id" to the first input found! So much for small mistakes in big code! Thanks everyone for helping. If not for @no.good.at.coding I would still be with no answer

IzzyCooper
  • 586
  • 1
  • 5
  • 14
0

As far as I understand, you would like to submit the form on toggling true of the check box in each row.

I would suggest you to get the form of the row and submit it rather than submitting all the forms using the form selector. Here is the code:

$(input).each(function() {
    $(this).click(function update() {
        if($(this).attr('checked')) {
            isDone = 1;
            $(this).parents("tr").find(form).submit();
            $(this).parents('tr').removeClass('unDone');
        }
        else {
            isDone = 0;
            //$(form).submit();
            $(this).parents('tr').addClass('unDone');
        }
    });
});

I did not understand the need to submit the form on uncheck operation of the checkbox. So, I have commented the form submit in uncheck operation. Also, use the even.preventDefault() than returning false in the form submit operation.

James Jithin
  • 10,183
  • 5
  • 36
  • 51
  • No, by submitting the form I am toggling a column in the database between true and false. this is the form 'trigger'. thanks! – IzzyCooper Apr 28 '11 at 19:43
0

I'd simplify your var input. It doesn't match the HTML you provided as closely as it may need. I'd simplify it to this:

var input = 'input[type="checkbox"]'; // probably specific enough

I would also use $(input).bind() or $(input).live() instead of .each(). I think it will work a bit more like what you are looking for.

http://api.jquery.com/bind/

http://api.jquery.com/live/

Dan Sorensen
  • 11,403
  • 19
  • 67
  • 100