Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery binding a form submit via a class name does not work

Tags:

jquery

ajax

I have a jQuery AJAX call that creates and displays a form like this in PHP:

echo '<form class="add_suggested_solution_comment" name="suggest_solution_comment_form" method="post">';
echo '<p><textarea name="suggested_solution_comment" cols=65 rows=6 ></textarea></p>';
echo '<input type="hidden" name="problem_id" value="'.$problem_id.'" />';                                                   echo '<input type="hidden" name="suggestion_id" value="'.$suggested_solution_id.'" />';   
echo '<p><input type="submit" class="button" value="Add Comment"></input></p>';             
echo '</form>';

Then when the user clicks the form, I try to catch the click in jQuery like this by using the class attribute of the form. For a specific reason, I can't use the id attribute of the form.

Here is how that jQuery looks like:

$('.add_suggested_solution_comment').bind('submit',function()

But for some reason when the user submits the form, the jQuery never gets triggered. Any idea why?

Thanks!!

like image 818
GeekedOut Avatar asked Feb 12 '26 09:02

GeekedOut


2 Answers

If you're creating the form dynamically via AJAX, you need to bind the submit handler using jQuery's live() method:

$('.add_suggested_solution_comment').live('submit', function(){
    alert('form submitted');
    // other code
});
like image 103
Colin Brock Avatar answered Feb 14 '26 11:02

Colin Brock


Are you firing this on load? try

$(document).ready(function(){
    $('.add_suggested_solution_comment').bind('submit',function()
});
like image 22
Chris Whittington Avatar answered Feb 14 '26 12:02

Chris Whittington



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!