javascript - 当 asp :TextBox Empty & no asp:RadioButton Is Selected 时禁用按钮

标签 javascript jquery asp.net

我有一个asp.net webform,我希望禁用我的提交按钮,如果我的 asp:TextBox为空,并且也没有为我的 asp:RadioButton 进行选择.

目前我的领域有以下内容,但我不知道如何加入单选按钮

JQuery

$(document).ready(function ()
        {
            $("#MainContent_btnRemoveUser").prop('disabled', true);
            $("#MainContent_btnAddUser").prop('disabled', true);
        });

        // Disables 'Remove' button unless all fields popluted
        $("#MainContent_txtRemoveUser").on("change", function ()
        {
            if ($('#MainContent_txtUsername').val())
            {
                $("#MainContent_btnRemoveUser").prop('disabled', false);
            }
            else
            {
                $("#MainContent_btnRemoveUser").prop('disabled', true);
            }
        });

HTML

        <div class="form-group">        
            <asp:Label runat="server" AssociatedControlID="txtRemoveUser" CssClass="col-sm-offset-2 col-sm-3 control-label">Enter Name To Be Removed</asp:Label>
            <div class="col-sm-3">
                <asp:TextBox runat="server" ID="txtRemoveUser" CssClass="form-control" />
            </div>
        </div>
        <div class="form-group">
            <asp:Label runat="server" CssClass="col-sm-offset-2 col-sm-3 control-label">
                <b>Request For</b>
            </asp:Label>
            <div class="col-sm-3">
                <label class="radio-inline">
                    <asp:RadioButton runat="server" ID="rbRemoveYourself" GroupName="RemoveRequestFor" /> Myself
                </label>
                <label class="radio-inline">
                    <asp:RadioButton runat="server" ID="rbRemoveOtherUser" GroupName="RemoveRequestFor" /> Other user
                </label>
            </div>
        </div>
        <div class="row">
            <div class="col-sm-offset-8 col-sm-3" style="padding-left: 0px">
                <asp:Button ID="btnRemoveUser" runat="server" Text="Remove From The List" CssClass="btn btn-danger" ToolTip="Click to remove the specified user from the payday lunch list." />
            </div>
        </div>

最佳答案

给你!添加了一个新函数来禁用该按钮,并将在文本框模糊和单选按钮更改时调用。

HTML

 <div class="form-group">        
                <asp:Label runat="server" AssociatedControlID="txtRemoveUser" CssClass="col-sm-offset-2 col-sm-3 control-label">Enter Name To Be Removed</asp:Label>
                <div class="col-sm-3">
                    <asp:TextBox runat="server" ID="txtRemoveUser" CssClass="form-control" />
                </div>
            </div>
            <div class="form-group">
                <asp:Label runat="server" CssClass="col-sm-offset-2 col-sm-3 control-label">
                    <b>Request For</b>
                </asp:Label>
                <div class="col-sm-3">
                    <label class="radio-inline">
                        <asp:RadioButton runat="server" ID="rbRemoveYourself" GroupName="RemoveRequestFor" /> Myself
                    </label>
                    <label class="radio-inline">
                        <asp:RadioButton runat="server" ID="rbRemoveOtherUser" GroupName="RemoveRequestFor" /> Other user
                    </label>
                </div>
            </div>
            <div class="row">
                <div class="col-sm-offset-8 col-sm-3" style="padding-left: 0px">
                    <asp:Button ID="btnRemoveUser" runat="server" Text="Remove From The List" CssClass="btn btn-danger" ToolTip="Click to remove the specified user from the payday lunch list." />
                </div>
            </div>

JavaScript

<script>
        $(document).ready(function () {
            $("#MainContent_btnRemoveUser").prop('disabled', true);
            $("#MainContent_btnAddUser").prop('disabled', true);
        });

        $("#MainContent_txtRemoveUser").on("blur", function () {
            disableButton();
        });

        $("#MainContent_rbRemoveYourself").change(function () {
            disableButton();
        });

        $("#MainContent_rbRemoveOtherUser").change(function () {
            disableButton();
        });

        // Disables 'Remove' button unless all fields popluted
        function disableButton() {

            var isRbRemoveSelfChecked = $("#MainContent_rbRemoveYourself").is(':checked')

            var isRbRemoveOtherChecked = $("#MainContent_rbRemoveOtherUser").is(':checked')

            if ($('#MainContent_txtRemoveUser').val() != '' && (isRbRemoveSelfChecked || isRbRemoveOtherChecked)) {
                $("#MainContent_btnRemoveUser").prop('disabled', false);
            }
            else {
                $("#MainContent_btnRemoveUser").prop('disabled', true);
            }
        }
    </script>

关于javascript - 当 asp :TextBox Empty & no asp:RadioButton Is Selected 时禁用按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33478736/

相关文章:

asp.net - 使用 Asp.net MVC4 配置 React,出现“无法在浏览器中加载资源[app.jsx]”之类的错误

javascript - 从两个 AJAX 请求构造一个 JavaScript 对象

javascript - Firefox WebExtension 选项页面 - 如何从 storage.local 保存/恢复首选项?

javascript - 通过单击元素并将该属性传递给字段,让输入字段识别它已被填充

jquery文件上传-IE完成回调data.result问题

asp.net - Excel 互操作库与 ASP.NET 不兼容?

c# - 无法将字符串作为 HTTPContent 发送

javascript - Kendo UI Grid 过滤后一次刷新排序

javascript - 在不损害页面响应能力的情况下设置 col-md-* div 类之间的边距

javascript - 使用 Jquery 检查类是否存在于页面上