启用禁用按钮 asp .net - 使用 javascript

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7602934/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 00:40:27  来源:igfitidea点击:

Enabling Disabling button asp .net - using javascript

javascriptasp.netbuttontextbox

提问by Varun Sharma

Currently in my application I am having a button and a text box. The user types something in the textbox and then presses the button. What I want is that:

目前在我的应用程序中,我有一个按钮和一个文本框。用户在文本框中键入内容,然后按下按钮。我想要的是:

The search button should should stay disabled when the page loads for the first time. I can achieve that by setting it to disabled in the code behind file.

首次加载页面时,搜索按钮应保持禁用状态。我可以通过在代码隐藏文件中将其设置为禁用来实现这一点。

Now I want it to remain disabled when the user types upto 2 characters. The moment the user types third character the button should get enabled automatically.

现在我希望它在用户最多输入 2 个字符时保持禁用状态。当用户输入第三个字符时,按钮应该自动启用。

The important thing is that it has to be done without asp .net AJAX since this website will be run from old mobile phones. So only pretty basic javascript or jquery is supported.

重要的是它必须在没有 asp .net AJAX 的情况下完成,因为该网站将在旧手机上运行。所以只支持非常基本的 javascript 或 jquery。

Any help will be appreciated.

任何帮助将不胜感激。

Thanks

谢谢

Varun

瓦伦

采纳答案by Joel WZ

in order to use the document.getElementById in asp.net and not have to use the full name, you should let asp.net provide it. Instead of: document.getElementById("ctl00_ctl00_phContent_phPageContent_btnSearch")

为了在asp.net 中使用document.getElementById 而不必使用全名,您应该让asp.net 提供它。代替: document.getElementById("ctl00_ctl00_phContent_phPageContent_btnSearch")

Try:

尝试:

document.getElementById('<%= btnName.ClientID %>')

document.getElementById('<%= btnName.ClientID %>')

Where btnNameis the asp:ButtonId. The <%=code will generate the actual button id, fully qualified, so you don't have to worry about things changing with the hard coded id.

哪里btnNameasp:ButtonId。该<%=代码将产生实际的按钮的ID,完全合格的,所以你不必事情与硬编码ID变更担心。

回答by Decker97

I got this to work with a HTML text box, I don't think you can do it with a asp.net text box:

我让它与 HTML 文本框一起工作,我认为你不能用 asp.net 文本框来做到这一点:

<head>
    <script type="text/javascript">
        function TextChange() {
            var t = document.getElementById("Text1");
            var b = document.getElementById("Button1");
            if (t.value.length > 2) {
                b.disabled = false;
            }
            else {
                b.disabled = true; 
            }


        }

    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <input id="Text1" type="text"  onkeyup="TextChange();" />
        <asp:Button ID="Button1"   runat="server" Text="Button" Enabled="False" />
    </div>
    </form>
</body>

回答by kand

If you're using jQuery, use

如果您使用 jQuery,请使用

$(<selector>).val().length 

to get the size, then you can set the button's disabled attribute with

获取大小,然后您可以设置按钮的禁用属性

$(<button selector>).attr('disabled',false).