最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

javascript - jQuery.each(values, function(i,v){ ... }) Why v is undefined? - Stack Overflow

matteradmin4PV0评论

I have several textboxes which I need to check before post them back. I do this with jQuery using each function. When the submit button is pressed (LinkButton1) I iterate over the textboxes and check they values:

<asp:textbox id="txt1" class="tocheck" runat="server />
<asp:textbox id="txt2" class="tocheck" runat="server />
<asp:textbox id="txt3" class="tocheck" runat="server />

$('#LinkButton1').click(function () {
    var error = false;
    $.each('.tocheck', function (i, v) {
        checkVal(v.val());
    });
});

But a runtime error is thrown saying v is undefined:

How can I retrieve the textbox value?

Thank you.

I have several textboxes which I need to check before post them back. I do this with jQuery using each function. When the submit button is pressed (LinkButton1) I iterate over the textboxes and check they values:

<asp:textbox id="txt1" class="tocheck" runat="server />
<asp:textbox id="txt2" class="tocheck" runat="server />
<asp:textbox id="txt3" class="tocheck" runat="server />

$('#LinkButton1').click(function () {
    var error = false;
    $.each('.tocheck', function (i, v) {
        checkVal(v.val());
    });
});

But a runtime error is thrown saying v is undefined:

How can I retrieve the textbox value?

Thank you.

Share Improve this question edited Nov 21, 2012 at 16:17 anmarti asked Nov 21, 2012 at 16:12 anmartianmarti 5,14310 gold badges61 silver badges96 bronze badges 4
  • I don't think you have provided enough HTML source. Your each iterates through .text, which I cannot see in your source code? – Curtis Commented Nov 21, 2012 at 16:13
  • I was mistaken typing the code, text means toeach. Post updated. – anmarti Commented Nov 21, 2012 at 16:14
  • Which version of jQuery are you using? With jQuery 1.7.1, it parses '.tocheck'as an array of letters (i = 0 -> v = '.', i = 1 -> v = 't'...). You have an $ missing perhaps? – Samuel Caillerie Commented Nov 21, 2012 at 16:17
  • Dont be confuse with $().each() and $.each() – A. Wolff Commented Nov 21, 2012 at 16:24
Add a ment  | 

6 Answers 6

Reset to default 6

You don't need to pass in v - remove it.

And use $(this) instead.

I.E

$('#LinkButton1').click(function () {
    var error = false;
    $('.tocheck').each( function () {
        checkVal($(this).val());
    });
});

The issue is that you aren't passing in a collection correctly.

$('#LinkButton1').click(function () {
    var error = false;
    $.each('.text', function (i, v) { // <-- you are passing in a STRING not a collection of elements
        checkVal(v.val());
    });
});

try

$('#LinkButton1').click(function () {
    var error = false;
    $.each($('.text'), function (i, v) {
        checkVal($(v).val());// <-- need to wrap in jQuery to use jQuery methods
    });
});

you got a error in the class selector. change it to

$(".tocheck").each(function(i, v){
    checkVal($(v).val());
})

Firstly

$.each('.tocheck', function (i, v) {
        checkVal(v.val());
    });

supposed to be either

$('.tocheck').each(function (i, v) {
        checkVal(v.value);
});

' OR /

 $.each( $('.tocheck'), function (i, v) {
            checkVal(v.value);
        });

Secondly v here is the DOM object and you are trying to use jQuery method on it .. That's the reason for the error..

So v.val()

supposed to be

v.value OR $(v).val(); OR this.value OR $(this).val();

The $.each function designed for mostly value arrays like intergers or string that s why v is value not item. so you should use each jQuery array as below.

$(".tocheck").each(function (index, item)
{
   alert($(item).val());
});
$('.tocheck').each( function () {
        checkVal($(this).val());
    });
Post a comment

comment list (0)

  1. No comments so far