javascript - 使用 javascript 启用和禁用

标签 javascript asp.net

我在页面中有按钮和文件上传控件,我想根据文件上传控件的值启用或禁用按钮。如果文件上传没有选择文件,那么按钮仍然禁用。

代码工作正常,但是如果您选择文件然后启用按钮(正确行为),然后您单击文件上传控件,但这次您想要单击取消但按钮未禁用。

function enableButton() {
        var a = document.getElementById("<%=file_upload.ClientID%>")
        if (a == 0) {
            document.getElementById("<%=btnAdd.ClientID%>").disabled = true;
        }
        else {
            document.getElementById("<%=btnAdd.ClientID%>").disabled = false;
        }

   }


<asp:FileUpload ID="file_upload" runat="server" AllowMultiple="true" maxLength="10" accept="text/plain" Enabled="false" CssClass="cssFileUpload" onchange="enableButton()"/>

  <asp:Button ID="btnAdd" runat="server" Text="Add Files" onclick="btnAdd_Click" Enabled="false" />

最佳答案

如果没有找到任何元素,document.getElementById 将返回 null。 使用

if( a == null) {}

关于javascript - 使用 javascript 启用和禁用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21154832/

相关文章:

asp.net - 为什么 HttpContext.Response.Cookies ["foo"] 添加一个cookie?

javascript - 在外部文件和目录结构中存储 jquery 代码?

javascript - TextInput 的 React-Native Expo 问题

c# - 空文本框是否被视为空字符串或 null?

c# - 获取一组 HtmlInputRadioButton 控件的选中值

javascript - Recaptcha 弹出警告

c# - ConfigurationManager.AppSettings ["SMTP"];空异常

javascript - 使用 webpack React 时图像总是出现损坏

javascript - 具有功能的 Angular UI Bootstrap Typeahead 模板 ng-src

graphics - 分层流程图的 Javascript 库