gpt4 book ai didi

jquery - 指令 AngularJS 仅输入 1 到 10 范围内的数字,jquery 指令 AngularJS

转载 作者:行者123 更新时间:2023-12-01 08:40:24 28 4
gpt4 key购买 nike

我是 angularjs 世界的新手,我只需要在数字类型的输入中输入从 1 到 10 的数字。不使用 HTML5 的 min 和 max 属性

我在 Jquery 中找到了一个示例,能否帮我将其转换为 angularjs 指令,其各自的参数是:

<input type = "number" directive-range = "1,10">

我在 Jquery 中找到的示例如下。 jquery 中的示例无需 html5 的 min 和 max 属性即可工作,这正是我所需要的:

<input type = "number" value = "1" min = "1" max = "10">
<p style = "display: none"> Accepting Only values ​​1-10 </ p>

var t = false

$ ('input'). focus (function () {
    var $ this = $ (this)
    
    t = setInterval (

    function () {
        if (($ this.val () <1 || $ this.val ()> 10) && $ this.val (). length! = 0) {
            if ($ this.val () <1) {
                $ this.val (1)
            }

            if ($ this.val ()> 10) {
                $ this.val (10)
            }
            $ ('p'). fadeIn (1000, function () {
                $ (this) .fadeOut (500)
            })
        }
    }, fifty)
})

$ ('input'). blur (function () {
    if (t! = false) {
        window.clearInterval (t)
        t = false;
    }
})

Example Jquery

我希望你能帮助我,我对 angularjs 指令不太了解

最佳答案

您可以使用 input of type number 的 native HTML5 属性: minmax .

在幕后 AngularJS 将使用 min-lengthmax-length指令。

只要您可以使用这些,我强烈建议您这样做,而不是创建新指令。

关于jquery - 指令 AngularJS 仅输入 1 到 10 范围内的数字,jquery 指令 AngularJS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48116619/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com