Javascript HTMLInputElement 没有方法“val”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3332698/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 04:12:36  来源:igfitidea点击:

HTMLInputElement has no method 'val'

javascriptjqueryhtml

提问by The.Anti.9

I'm looping through cells in a table row. each cell has a text box in it, and I want to take the value of the text box and push it onto an array.

我正在遍历表格行中的单元格。每个单元格中都有一个文本框,我想获取文本框的值并将其推送到数组中。

function dothing() {
        var tds = $('#'+selected+' td');
        var submitvals = new Array();
        tds.each(function(i) {
            var val = $(this).children('input')[0].val();
            submitvals.push(val);
        });
    }

Theres more to the function, but this is all that is relevant. For some reason, when I run this code, I get "HTMLInputElement has no method 'val'." I thought that input elements were supposed to have a val()method in jQuery that got the value so this makes no sense. Am I missing something, or doing it wrong?

该功能还有更多内容,但这就是相关的全部内容。出于某种原因,当我运行此代码时,我得到“HTMLInputElement has no method 'val'”。我认为输入元素应该val()在 jQuery 中有一个方法来获取值,所以这是没有意义的。我错过了什么,还是做错了?

回答by meder omuraliev

val()is a jQuery method. .valueis the DOM Element's property. Use [0].valueor .eq(0).val()....

val()是一个 jQuery 方法。.value是 DOM 元素的属性。使用[0].value.eq(0).val()....

回答by Alex

.val()is a jQuery function, not a javascript function. Therefore, change:

.val()是一个 jQuery 函数,而不是一个 javascript 函数。因此,更改:

var val = $(this).children('input')[0].val()

To:

到:

var val = $(this).children('input:eq(0)').val()

回答by edtsech

function dothing() {
    var tds = $('#'+selected+' td');
    var submitvals = new Array();
    tds.each(function(i) {
        var val = $($(this).children('input')[0]).val();
        submitvals.push(val);
    });
}

回答by Ben Rowe

.val() is a jquery method. Using [0] returns the DOM element, not the jquery element

.val() 是一种 jquery 方法。使用 [0] 返回 DOM 元素,而不是 jquery 元素

var val = $(this).children('input:first').val();

回答by Dronz

What I don't understand, is why none of the suggested syntaxes on this or other questions similar to this seem to work for me. I had to do trial and error and eventually had to use:

我不明白的是,为什么关于这个或其他类似问题的建议语法似乎对我不起作用。我不得不反复试验,最终不得不使用:

MySelectElement.value = x;

MySelectElement.value = x;

It also didn't help that the Visual Studio Intellisense suggestions offer a whole other range of unworking method names, such as ValueOf().

Visual Studio Intellisense 建议提供了一系列其他无效的方法名称,例如 ValueOf(),这也无济于事。