【问题标题】:jQuery/asp.net - Enable/Disable button only after user enters text in the textboxes and selects a value from the dropdown listjQuery/asp.net - 仅在用户在文本框中输入文本并从下拉列表中选择值后启用/禁用按钮
【发布时间】:2018-01-17 16:26:36
【问题描述】:

这是控件的 aspx 代码我想要在这里的功能是,当我输入文本框的值并选择一个下拉值时.. 只有在那之后才能启用按钮否则该按钮应该被禁用。

<div id="div1" class="panel-collapse collapse in">
    <div class="form-inline">
        <div class="form-group">
            <label class="control-label">Part Number:</label>
            <asp:TextBox type="text" class="form-control" ID="itemOne" runat="server" />
        </div>
        <div class="form-group">
            <label>Name:</label>
            <asp:TextBox type="text" class="form-control" ID="itemTwo" runat="server" />
        </div>
        <div class="form-group">
            <label>Price:</label>
            <asp:TextBox type="text" class="form-control" ID="itemThree" runat="server" />
        </div>
        <div class="form-group">
            <label>Weight:</label>
            <asp:TextBox type="text" class="form-control" ID="itemFour" runat="server" Style="width: 100px" />
        </div>
        <div class="form-group">
            <label>Quantity:</label>
            <asp:TextBox type="text" class="form-control" ID="miscItemQuantity" runat="server" Style="width: 80px" />
        </div>
        <div class="form-group">
            <asp:DropDownList ID="dropdownList" CssClass="form-control" runat="server" Width="150" />
            <%-- Items are added dynamically from the codebehind --%>
        </div>
        <asp:Button runat="server" ID="addItem" type="submit" Text="Add Item" CssClass="btn btn-primary pull-right"></asp:Button>
    </div>
</div>

这是我尝试使用一个文本框启用和禁用按钮的 jQuery 代码

$(document).ready(function () {
    $("#<%=itemOne.ClientID%>").keyup(function () {
        if ($(this).val().length > 2) {
            $("#<%=addItem.ClientID%>").removeAttr('disabled');
        }
        else {
            $("#<%=addItem.ClientID%>").attr('disabled', 'disabled');
        }
    }).trigger('keyup');
});

我很高兴收到任何解决方案。

【问题讨论】:

  • 使用输入事件并检查文本。还攻击选择的事件以检测选择更改

标签: c# jquery asp.net


【解决方案1】:

我认为您正在寻找类似的东西。首先为div1 中的每个TextBox 和DropDownList 添加一个监听器以检查更改。然后使用 div 验证所有控件的正确值,并相应地设置按钮可见性。

<script type="text/javascript">
    $(document).ready(function () {
        $("#div1 input[type='text']").keyup(function () {
            checkValues();
        });

        $("#div1 select").change(function () {
            checkValues();
        });

        checkValues()
    });

    function checkValues() {
        var isValid = true;

        $("#div1 input[type='text']").each(function () {
            if ($(this).val().length < 2) {
                isValid = false;
            }
        });

        $("#div1 select").each(function () {
            if ($(this).prop('selectedIndex') === 0) {
                isValid = false;
            }
        });

        if (isValid) {
            $("#<%=addItem.ClientID%>").removeAttr('disabled');
        }
        else {
            $("#<%=addItem.ClientID%>").attr('disabled', 'disabled');
        }
    }
</script>

【讨论】:

  • 非常感谢您的解决方案。我对您提供的解决方案进行了一些更改,效果很好。在完成对它添加一些更改后,我将发布我的解决方案。再次感谢。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-02-27
  • 2014-06-04
  • 1970-01-01
  • 2017-06-26
  • 1970-01-01
相关资源
最近更新 更多