【问题标题】:How to fix jquery .is(':focus') not responding如何修复 jquery .is(':focus') 没有响应
【发布时间】:2019-12-12 13:51:50
【问题描述】:

我正在做一个 if 语句,但它似乎不起作用。我认为这与 :focus (已实现 jquery)有关

我尝试使用 $("input:focus") 但是当我在任何地方按 enter 时,(无论是否集中)它都会显示警报。

<script type="text/javascript" src="script.js"></script>

<body style="background-color: #90EE90;">

    <center>
        Sample text
        <br><br><br><br>
        <input type="text" name="user-input" placeholder="insert name">
        <br><br>

    </center>
    <script>

        var input = $('#user-input').val();

        $(document).ready(function () {
            $(document).keypress(function (key) {
                if (key.which === 13 && $('#user-input').is(':focus')) {
                    alert("hello");
                }
            })
        })
    </script>
</body>

当我在用户输入突出显示时按 Enter 键时,它应该会发出 Hello 警报,但实际上什么也没发生。

【问题讨论】:

  • 你选择了$('#user-input')的输入元素,但是该元素没有id。

标签: javascript


【解决方案1】:

name="user-input" 更改为id="user-input"

【讨论】:

    猜你喜欢
    • 2019-10-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-06
    相关资源
    最近更新 更多