简体   繁体   English

jQuery.each(values,function(i,v){…})为什么v未定义?

[英]jQuery.each(values, function(i,v){ … }) Why v is undefined?

I have several textboxes which I need to check before post them back. 我有几个文本框,需要先检查它们,然后再发布回去。 I do this with jQuery using each function. 我使用每个函数使用jQuery。 When the submit button is pressed (LinkButton1) I iterate over the textboxes and check they values: 当按下提交按钮(LinkBut​​ton1)时,我遍历文本框并检查它们的值:

<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: 但是抛出了运行时错误,表明v未定义:

在此处输入图片说明

How can I retrieve the textbox value? 如何检索文本框值?

Thank you. 谢谢。

You don't need to pass in v - remove it. 您无需传递v将其删除。

And use $(this) instead. 并改用$(this)

IE IE浏览器

$('#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.. 其次,这里的vDOM对象 ,您正尝试在其上使用jQuery方法 ..这就是错误的原因。

So v.val() 所以v.val()

supposed to be 应该是

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

The $.each function designed for mostly value arrays like intergers or string that s why v is value not item. $.each函数主要用于值数组,例如整数或字符串,这就是为什么v不是值的原因。 so you should use each jQuery array as below. 因此,您应该使用每个jQuery数组,如下所示。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM