How do I get a specific form value in jquery using the parent $self click function
I have a form that looks something like this
<form action="#" class="ajax_form" method="post" id="comment_form" accept-charset="utf-8">
<textarea name="comment" id="form_comment"></textarea开发者_如何学运维>
<input name="another_field" value="some data" type="text" id="form_another_field" />
<input name="method" value="some_method" type="hidden" id="form_method" />
<input name="submit" value="Submit" type="submit" id="form_submit" />
</form>
and a jquery submit listener for any class named ajax_form
$(document).ready(function() {
$('.ajax_form').submit(function() {
alert(JSON.stringify($(this).serializeObject()));
alert($("input:form_method").val());
return false;
});
});
The first part of the submit function serlializes the form elements and puts them into a json string for an ajax request. But I need to extract the value of id #form_method before I make the ajax request but can't seem to figure out how to do it with reference to using $this. I tried getting the form children of $this but failed doing that as well.
You're missing the # in your selector. This should do the trick:
$("#form_method").val();
You should try $("input#form_method").val()
instead of $("input:form_method").val()
In fact only $("#form_method").val()
should perfectly do the job.
Eventually you may put specify the context of your search with $("#form_method",this).val()
if you have multiple ajax_form on the page, but in such case you should reconsider using multiple time the same id as an id has to be unique
精彩评论