【问题标题】:Disable an asp net Button with javascript使用 javascript 禁用 asp net 按钮
【发布时间】:2018-10-10 17:55:14
【问题描述】:

我有一个局部视图,我想在某些情况下禁用按钮。

<td style="padding-left: 30px;">
  <asp:Button ID="AddProdCostLine" runat="server" CausesValidation="False" Text="Add Line" CssClass="buttonBlue"></asp:Button>
</td>

在我的 javascript 中,我需要检查一些文本框是否已填充,如果没有,用户不应该能够触发按钮(通过禁用它)

事实上,我需要在相同的条件下强制设置一个文本框。那是我的想法。 我找不到禁用按钮的方法,这里是代码:

function LoadComponentProdCost() {
    $(function () {
        $('input[id*="ProductionCostLineField"]').blur(function () {
            var amount = this.value;
            var textInInvoice = 'Mandatory';
            $('input[id*="ProductionCostInvoiceToLineField"]').each(function () {
                if (amount == '' || amount == '0') {
                    textInInvoice = '';
                } else {
                    textInInvoice = 'Mandatory';
                    alert("You must inform the field 'Invoiced By'");
                    //doesn't work I need here to disable the button 
                    document.getElementById("<%=AddProdCostLine.ClientID%>").disabled = "disabled";
                    document.getElementById('<%= AddProdCostLine.ClientID %>').disabled = true;
                    document.getElementById("<%=AddProdCostLine.ClientID%>").setAttribute("disabled", "disabled");
                }
            });
            $('input[id*="ProductionCostInvoiceToLineField"]').val(textInInvoice);
        });
    });

我的 Javascript 在部分视图文件 .ascx 中

组件的渲染 HTML 是这样的:

<input name="ProdCostControl$ProdCostGrid$ctl02$ProductionCostInvoiceToLineField" type="text" maxlength="100" id="ProdCostControl_ProdCostGrid_ctl02_ProductionCostInvoiceToLineField" tabindex="25" style="width:269px;">

【问题讨论】:

  • 你试过document.getElementById("&lt;%=AddProdCostLine.ClientID%&gt;").setAttribute("disabled", "disabled");吗?
  • 此函数是否包含在外部 javascript 文件中?意思是,在视图之外?
  • 请在您的LoadComponentProdCost 函数中发布您的渲染 HTML,包括HTML 表单和渲染字符串。
  • @Benoît 上述代码是否执行?什么时候调用 LoadComponentProdCost()?
  • @mac9416 是的,那是我复制和粘贴的时候。这不是我当前的代码

标签: javascript asp.net validation button


【解决方案1】:

通过使用 alert() 函数,我设法调试/检查我的组件是否为空,所以这是我在上面发布的代码的解决方案:

function LoadComponentProdCost() {
    $(function () {
        $('input[id*="ProductionCostLineField"]').blur(function () {
            var amount = this.value;
            $('input[id*="ProductionCostInvoiceToLineField"]').each(function () {
                var textInvoicedBy = this.value;
                if (amount == '' || amount == '0') {
                    document.getElementById("<%=AddProdCostLine.ClientID%>").className = 'buttonBlue';
                } else {
                    if (this.value != '' || amount == '' || amount == '0') {
                    document.getElementById("<%=AddProdCostLine.ClientID%>").className = 'buttonBlue';
                    document.getElementById('<%= AddProdCostLine.ClientID %>').disabled = false;
                    }
                    if ((amount != '' || amount != '0') && textInvoicedBy == '') {
                        alert("You must inform the field 'Invoiced By'");
                        document.getElementById("<%=AddProdCostLine.ClientID%>").className = 'buttonLightGray3';
                        document.getElementById('<%= AddProdCostLine.ClientID %>').disabled = true;
                    }
                }
            });
        });
    });
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-03-17
    • 2016-08-19
    • 1970-01-01
    • 1970-01-01
    • 2016-08-16
    • 2012-07-26
    • 2014-03-31
    相关资源
    最近更新 更多