禁用按钮当asp:TextBox Empty&没有asp:RadioButton被选中

时间:2015-11-02 13:29:28

标签: javascript jquery asp.net

我有一个asp.net网络表单,如果我的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>

3 个答案:

答案 0 :(得分:1)

你去吧!添加了一个新功能来禁用该按钮,将在模糊文本框和更改单选按钮时调用。

<强> 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>

答案 1 :(得分:1)

这应该会给你预期的结果: -

$('input[name="RemoveRequestFor"]').change(function () {
    if($("#MainContent_txtUsername").val().trim() != "")
         $("#MainContent_btnAddUser").prop('disabled', false);
});

$("#MainContent_txtUsername").blur(function () {
    if($('input[name="RemoveRequestFor"]:checked').length > 0)
         $("#MainContent_btnAddUser").prop('disabled', false);
});

此外,您可以使用keyup功能代替blur来提高表单的响应速度。

答案 2 :(得分:0)

尝试用{替换if ($('#MainContent_txtUsername').val())  if ($('#MainContent_txtUsername').val() != '')

对于单选按钮,不要检查onchange上的任何条件并写入相同的逻辑。