为什么jQuery输入元素和JavaScript输入元素之间有区别?
我有两种类型的呼叫函数check_negation_value()
。 在第一个类型中,我设置onChange = change_quantity(this)
,然后调用check_negative_value()
从那里开始,例如:
function change_quantity(elem) {
check_negative_value(elem);
console.log(elem);
}
sectsect i sectsect i设置dectement_cart(this)
,然后调用check_negative_value()
从那里喜欢:
function decrement_cart(elem) {
const input = $(elem).next('input');
check_negative_value(input);
console.log(input);
}
和check_negation_value()
我必须使用input.value.value
或> input.val()
!
function check_negative_value(input){
if(input.value <= 0){
alert('not accept value under 1');
input.value = 1;
}
}
或:
function check_negative_value(input){
if(input.val() <= 0){
alert('not accept value under 1');
input.val(1);
}
}
因为每个调用函数都有不同的元素,并且console.log
的结果是: 在jQuery呼叫中:
r.fn.init [input.input-number.text-center, prevObject: r.fn.init(1)]
在JavaScript呼叫中:
<input class="input-number text-center" type="number" value="-1" min="0" max="1000" onchange="change_quantity(this)">
HTML代码是:
<div class="input-group-button" onclick="decrement_cart(this)">
<span class="input-number-decrement">-</span>
</div>
<input class="input-number text-center" type="number" min="0" max="1000"
onchange="change_quantity(this)">
<div class="input-group-button" onclick="increment_cart(this)">
<span class="input-number-increment">+</span>
</div>```
我如何在check_negative_value()
中使用输入元素, input.val() .value
???
谢谢
I have two type of call function check_negative_value()
.
in first type I set the onchange=change_quantity(this)
and then call check_negative_value()
from there like:
function change_quantity(elem) {
check_negative_value(elem);
console.log(elem);
}
and in second type I set the decrement_cart(this)
and then call check_negative_value()
from there like:
function decrement_cart(elem) {
const input = $(elem).next('input');
check_negative_value(input);
console.log(input);
}
and in check_negative_value()
I must use input.value
or input.val()
!!
function check_negative_value(input){
if(input.value <= 0){
alert('not accept value under 1');
input.value = 1;
}
}
Or :
function check_negative_value(input){
if(input.val() <= 0){
alert('not accept value under 1');
input.val(1);
}
}
because each calling function have different element and the result of console.log
is this:
in jquery call:
r.fn.init [input.input-number.text-center, prevObject: r.fn.init(1)]
in javascript call:
<input class="input-number text-center" type="number" value="-1" min="0" max="1000" onchange="change_quantity(this)">
the html code is :
<div class="input-group-button" onclick="decrement_cart(this)">
<span class="input-number-decrement">-</span>
</div>
<input class="input-number text-center" type="number" min="0" max="1000"
onchange="change_quantity(this)">
<div class="input-group-button" onclick="increment_cart(this)">
<span class="input-number-increment">+</span>
</div>```
how can I use input element in check_negative_value()
without difference between input.val()
and input.value
???
thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
javaScript和jQuery都不具有
输入
元素。input
元素是 dom dom dom 由主机环境(浏览器)提供,而不是语言(JavaScript)或库(jQuery)。您看到的区别是DOM对象(带有
value
属性)与围绕它的jQuery包装对象(带有val
方法)。您可以通过查看它是否具有
jQuery
属性,或者在这种情况下更直接地检查它是否是jQuery包装器,如果它具有val
属性:注意:
val()
返回字符串(除非jQuery集为空),而不是数字(value value
的值也始终是字符串)。尽管&lt; = 0
将隐式转换它,但您可以考虑故意转换它,因为隐式转换假设“”
应该为0
;在我的答案中,更多内容在这里。Neither JavaScript nor jQuery has an
input
element.input
elements are DOM objects provided by the host environment (browser), not the language (JavaScript) or library (jQuery).The difference you're seeing is the DOM object (with a
value
property) vs. a jQuery wrapper object around it (with aval
method).You can check to see whether what you have is a jQuery wrapper by seeing if it has a
jquery
property or perhaps more directly in this case if it has aval
property:Side note:
val()
returns a string (unless the jQuery set is empty), not a number (the value ofvalue
is also always a string). Although<= 0
will implicitly convert it, you might consider converting it on purpose as implicit conversion assumes""
should be0
; more in my answer here.