检查按钮是否被禁用

发布于 2025-01-04 23:24:45 字数 820 浏览 1 评论 0原文

我正在开发一个应用程序,用户可以猜测一个数字(7 个正确的数字)。该应用程序包含一个输入字段和一个按钮,如果用户没有更多猜测,则输入字段和按钮将被禁用,并且会出现一个新按钮(重新启动)。

在我的代码隐藏文件中是用于检查用户是否有更多猜测的代码,如果没有,则以下代码负责禁用/启用按钮:

代码隐藏文件:

    ...
       btnCheckNr.Enabled = false;
       inputBox.Enabled = false;
       newGame.Visible = true;
   ...

我没有使用 ViewState 而是 Session状态,并且每次完成邮包时,字段都会恢复到从开始时的状态,即。已启用。每次用户进行猜测时,输入字段都会获得焦点,并且其中的内容(例如最后一次猜测)会被选择。当字段和按钮被禁用时,这可以接受,因此我添加了一个检查来查看输入字段是否被禁用。如果是这样,则不应进行聚焦和选择(否则会出现错误)。

然而,使用这段代码,输入字段永远不会获得焦点,为什么呢?我是否做错了什么,在这种情况下如何实现?

提前致谢!

外部.js:

var Capsule = {

    init: function() {
        var input = $('#inputBox');

        if (!input.is(":disabled"))
            input.focus();
            input.select();
        }
    }
}

window.onload = Capsule.init;

I'm working on an application where the users guesses on a number (7 trues). The app contains of an input field as well as a button, and if the user hasn't got more guesses the input field as well as the button is disabled, and a new button appears (restart).

In my code behind-file is the code for checking if the user has more guesses or not, and if not the following code takes care of the disabling/enabling of the buttons:

code behind-file:

    ...
       btnCheckNr.Enabled = false;
       inputBox.Enabled = false;
       newGame.Visible = true;
   ...

I'm not using ViewState but Session state, and every time a postpack is done the fields is back as they were from the start, ie. enabled. Every time the user has made a guess the input field gets focus and the content inside (eg. last guess made) gets selected. This works accept for when the field and button gets disabled, and by that reason I've added a check to see if the input field is disabled or not. If so, focus and selection shall not be done (otherwise I get an error).

However, with this code the input field never gets focus, why is that? Is it something that I'm doing wrong and in that case how could this be accomplished?

Thanks in advance!

external.js:

var Capsule = {

    init: function() {
        var input = $('#inputBox');

        if (!input.is(":disabled"))
            input.focus();
            input.select();
        }
    }
}

window.onload = Capsule.init;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

2025-01-11 23:24:45

尝试改为触发点击:

input.click().select();

演示。

Try triggering a click instead:

input.click().select();

Demo.

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