Javascript val()函数给出控制台错误
Javascript val()函数给出控制台错误,javascript,jquery,input,numbers,Javascript,Jquery,Input,Numbers,input[type=“number”]有一个max属性,因此人们不能单击高于给定数字的数字。但这不包括键入的数字,它们可能远远超过给定的最大数字。因此,使用jQuery,我想为这个问题提供一个解决方案。我已经知道了: var productPageInput = $('#product-options-wrapper .option input[type="number"].qty'); var max = 60; productPageInput.attr('max'
input[type=“number”]
有一个max
属性,因此人们不能单击高于给定数字的数字。但这不包括键入的数字,它们可能远远超过给定的最大数字。因此,使用jQuery,我想为这个问题提供一个解决方案。我已经知道了:
var productPageInput = $('#product-options-wrapper .option input[type="number"].qty');
var max = 60;
productPageInput.attr('max', max);
productPageInput.bind('keyup input', function(e){
console.log($(this).val());
if( $(this).val() > max ){
$(this).val() = max;
}
});
但是,每次我执行此代码并键入60以上,就会出现以下控制台错误:
Uncaught ReferenceError: Invalid left-hand side in assignment(…)
哪个指向这条线:
$(this).val() = max;
这里出了什么问题?要指定值:
$(this).val(max);
通常,JavaScript中赋值运算符左侧的函数调用没有意义。要赋值:
$(this).val(max);
一般来说,JavaScript中赋值运算符左侧的函数调用没有意义。这使得JQuery可以设置输入值
$("input").val("value");
//or
$("input")[0].value = "value";
这可以通过JQuery设置输入值
$("input").val("value");
//or
$("input")[0].value = "value";