How should a given form input be accessed within jQuery validate()'s submitHandler callback? I have a convoluted solution, but expect there must be a better way.
/
$(document).ready(function(){
$("#myform").validate({
submitHandler: function(form) {
var inputs=$(form).find(':input');
console.log('inputs',inputs);
console.log('form',form,$(form),form.elements);
console.log('this',this, $(this));
//console.log( this.serializeArray() ); //this is not a jQuery object
console.log( $( this ).serializeArray() );
console.log( $( form ).serializeArray() );
console.log( inputs.serializeArray() );
alert(inputs.filter('[name=myName]').val());
}
});
});
<form id='myform' >
<input type='text' name='myName' value="123" />
<input type="submit" value="submit">
</form>
How should a given form input be accessed within jQuery validate()'s submitHandler callback? I have a convoluted solution, but expect there must be a better way.
http://jsfiddle/1tzuojpg/2/
$(document).ready(function(){
$("#myform").validate({
submitHandler: function(form) {
var inputs=$(form).find(':input');
console.log('inputs',inputs);
console.log('form',form,$(form),form.elements);
console.log('this',this, $(this));
//console.log( this.serializeArray() ); //this is not a jQuery object
console.log( $( this ).serializeArray() );
console.log( $( form ).serializeArray() );
console.log( inputs.serializeArray() );
alert(inputs.filter('[name=myName]').val());
}
});
});
<form id='myform' >
<input type='text' name='myName' value="123" />
<input type="submit" value="submit">
</form>
Share
Improve this question
asked Jun 7, 2016 at 15:25
user1032531user1032531
26.4k75 gold badges245 silver badges416 bronze badges
7
-
You have not explained the root problem at all or what you ultimately are trying to achieve. Typically one uses jQuery's
.serialize()
to get the form's data before submitting. Otherwise, a single input can be targeted and attached to.val()
to get its value. – Sparky Commented Jun 7, 2016 at 15:30 -
Why
inputs.filter('[name=myName]').val()
instead of simply$('[name=myName]').val()
? – Sparky Commented Jun 7, 2016 at 15:32 -
@Sparky Usually, I use
.serialize()
exactly as you say. For this time, however, I wish to get the value of a couple form inputs and add them to a table, and them send them to the server upon some other event. – user1032531 Commented Jun 7, 2016 at 15:33 -
Then target the input's value directly:
$('input[name="myName"]').val()
– Sparky Commented Jun 7, 2016 at 15:33 - @Sparky. Definitely better than what I was doing. Thanks! – user1032531 Commented Jun 7, 2016 at 15:35
1 Answer
Reset to default 5Target the input's value directly: $('#myform input[name="myName"]').val()
DEMO: http://jsfiddle/fz3ed89q/2/
Using the built-in form
argument: $(form).find('input[name="myName"]').val()
DEMO: http://jsfiddle/fz3ed89q/3/
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744842043a4596614.html
评论列表(0条)