javascript - Access form input values within jquery validate's submitHandler - Stack Overflow

How should a given form input be accessed within jQuery validate()'s submitHandler callback?I ha

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
 |  Show 2 more ments

1 Answer 1

Reset to default 5

Target 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条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信