user1032531 user1032531 - 5 months ago 10
jQuery Question

Access form input values within jquery validate's submitHandler

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.net/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>

Answer

Target the input's value directly: $('#myform input[name="myName"]').val()

DEMO: http://jsfiddle.net/fz3ed89q/2/

Using the built-in form argument: $(form).find('input[name="myName"]').val()

DEMO: http://jsfiddle.net/fz3ed89q/3/