简体   繁体   English

HTML5 / JavaScript / jQuery-使用jQuery选择器时,输入范围.value()变为未定义?

[英]HTML5/JavaScript/jQuery - Input Range .value() becomes undefined when using jQuery selector?

So I'm trying to make a input range object to allow the user to select from a range of values, and reading this and updating a jQuery variable. 因此,我试图制作一个输入范围对象,以允许用户从一系列值中进行选择,然后阅读并更新jQuery变量。

If I try to access the value of: 如果我尝试访问以下值:

<input id="scroll" type="range" min="1" max="60" value="5">

If I use: 如果我使用:

$("#scroll").on("change", function() {
    variable = this.value;
});

it functions correct and the variable updates. 它功能正确且变量更新。

However, if I use the jQuery tag selector, as follows: 但是,如果我使用jQuery标签选择器,如下所示:

$("#scroll").on("change", function() {
    variable= $("#scroll").value;
}

variable becomes undefined. 变量变为未定义。

Could you explain why? 你能解释为什么吗? My thought is that "this" is referring to the HTML object as it is updated, and uses the HTML attribute, but frankly I don't know enough about it. 我的想法是,“ this”在更新时引用HTML对象,并使用HTML属性,但是坦率地说,我对此并不了解。

The correct syntax is $("#scroll").val() . 正确的语法是$("#scroll").val() You left out the # and you used the DOM property instead of the jQuery method. 您省略了#并使用了DOM属性而不是jQuery方法。

$("#scroll").on("change", function() {
    variable= $("#scroll").val();
}

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

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