Get value from ajax-loaded form before submitting
I have an app that loads an ajax form when a button is clicked. I then want to check a开发者_如何转开发 value in that ajax loaded form called "comment_content" so that I don't send an ajax request if the comment_content is empty. I can capture the form submission using the live method, however, I can't capture the form element because it needs an element.
I was looking at livequery but was wondering if there is a more efficient way by using live or if that I shouldn't even bother? Your help is very much appreciated.
$("#comment_submit").live('click', function(){
var comment = $("#comment_content").html();
if(comment == ""){
$("#notice").html("").stop(true, true);
$("#notice").append("<p>Are you going to write something in that comment?</p>");
$('#notice').slideDown().delay(2000).slideUp();
return false;
}
return true;
});
I'm not totally sure I understand the question, but if I do understand correctly you can capture the form element #comment_content with this:
var contentElement = $('#comment_submit').parents('form').find('#comment_content');
You can then use the var contentElement for whatever you intended for. This should work anywhere provided the form has loaded. If you are just looking for the form element, you could use
var formElement = $('#comment_submit').parents('form');
Or just give the form an id/class and find it explicitly
精彩评论