为什么jQuery输入元素和JavaScript输入元素之间有区别?

发布于 2025-02-01 13:57:50 字数 1934 浏览 1 评论 0原文

我有两种类型的呼叫函数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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

云之铃。 2025-02-08 13:57:50

javaScript和jQuery都不具有输入元素。 input元素是 dom dom dom 由主机环境(浏览器)提供,而不是语言(JavaScript)或库(jQuery)。

您看到的区别是DOM对象(带有value属性)与围绕它的jQuery包装对象(带有val方法)。

我如何在check_negation_value()中使用输入元素,而input.val() and input.value ??? < /p>

您可以通过查看它是否具有jQuery属性,或者在这种情况下更直接地检查它是否是jQuery包装器,如果它具有val属性

function check_negative_value(input){
    if (input.jquery) {
        // It's a jQuery wrapper, get the first DOM element it contains from it
        input = input[0];
    }
    if (input.value <= 0){
        alert('not accept value under 1');
        input.value = 1;
    }
}

:注意: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 a val method).

how can I use input element in check_negative_value() without difference between input.val() and input.value ???

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 a val property:

function check_negative_value(input){
    if (input.jquery) {
        // It's a jQuery wrapper, get the first DOM element it contains from it
        input = input[0];
    }
    if (input.value <= 0){
        alert('not accept value under 1');
        input.value = 1;
    }
}

Side note: val() returns a string (unless the jQuery set is empty), not a number (the value of value is also always a string). Although <= 0 will implicitly convert it, you might consider converting it on purpose as implicit conversion assumes "" should be 0; more in my answer here.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文