繁体   English   中英

使用 jquery 启用/禁用对 Radio 选择的控制

[英]Enable/Disable Controls on selection of Radio using jquery

我正在尝试根据 Radio 的选择启用/禁用某些控件。 它似乎不起作用。 我希望在选择是收音机时启用禁用的文本和收音机字段。 请帮忙!

ASP 代码段

                <div class="form-group" >
                    <label class="control-label">Whether any other children of the same family is studying in Primary/ Secondary Section of SVVHS</label><br />
                    <input type="radio" value="yes" id="chkRelatedStudentYes" runat="server" name="RelatedStudent" required="required"/> <label class="control-label">Yes</label><br />
                    <input type="radio" value="no" id="chkRelatedStudentNo" runat="server" name="RelatedStudent" required="required"/> <label class="control-label">No</label>
                 </div>
                <div class="form-group">
                    <label class="control-label">Name of the Student</label>
                    <input maxlength="100" type="text" required="required" class="form-control" placeholder="Enter Name of the Related Student" id="txtRelatedStudentName" runat="server" disabled="disabled" />
                </div>
                <div class="form-group">
                    <label class="control-label">Section</label><br />
                    <input type="radio" value="Primary" id="chkPrimary" runat="server" name="Section"  required="required" disabled="disabled"/> <label class="control-label">Primary</label><br />
                    <input type="radio" value="Secondary" id="chkSecondary" runat="server" name="Section"  required="required" disabled="disabled"/> <label class="control-label">Secondary</label>
                </div>
                <div class="form-group">
                    <label class="control-label">Standard</label>
                    <input maxlength="12" type="text" required="required" class="form-control" placeholder="Enter Standard of the Related Student" id="txtStandard" runat="server" disabled="disabled"/>
                </div>
                <div class="form-group">
                    <label class="control-label">Division</label>
                    <input maxlength="12" type="text" required="required" class="form-control" placeholder="Enter Division of the Related Student" id="txtDivision" runat="server" disabled="disabled"/>
                </div>

jQuery 片段

        <script type="text/javascript">
            $('#chkRelatedStudentYes').click(function () {
                $('#txtRelatedStudentName').removeAttr("disabled");
                $('#chkPrimary').removeAttr("disabled");
                $('#chkSecondary').removeAttr("disabled");
                $('#txtStandard').removeAttr("disabled");
                $('#txtDivision').removeAttr("disabled");
            });
            $('#chkRelatedStudentNo').click(function () {
                $('#txtRelatedStudentName').attr("disabled", "disabled");
                $('#chkPrimary').attr("disabled", "disabled");
                $('#chkSecondary').attr("disabled", "disabled");
                $('#txtStandard').attr("disabled", "disabled");
                $('#txtDivision').attr("disabled", "disabled");
            });
</script>

嗨,您触发事件的方式是错误的

请试试这个,这是应该可以工作的完整代码

注:已测试

 $(document).ready(function() {
        $("input[name=RelatedStudent]:radio").click(function() { // attack a click event on all radio buttons with name 'radiogroup'


                if($(this).val() == 'yes') {//check which radio button is clicked 
                        $('#txtRelatedStudentName').removeAttr("disabled");
                $('#chkPrimary').removeAttr("disabled");
                $('#chkSecondary').removeAttr("disabled");
                $('#txtStandard').removeAttr("disabled");
                $('#txtDivision').removeAttr("disabled");
                } else if($(this).val() == 'no') {
                      $('#txtRelatedStudentName').attr("disabled", "disabled");
                $('#chkPrimary').attr("disabled", "disabled");
                $('#chkSecondary').attr("disabled", "disabled");
                $('#txtStandard').attr("disabled", "disabled");
                $('#txtDivision').attr("disabled", "disabled");
                } 
        });
});

你可以在这里试试https://jsfiddle.net/abhiyx/fgen1br9/

您可以使用 jQuery prop() 方法代替 removeAttr()。 尝试这个 -

$('#chkRelatedStudentYes').click(function () {
   $('#txtRelatedStudentName').prop("disabled", false);
   $('#chkPrimary').prop("disabled", false);
   $('#chkSecondary').prop("disabled", false);
   $('#txtStandard').prop("disabled", false);
   $('#txtDivision').prop("disabled", false);
});

我发现了问题所在。 脚本无法通过 ID 或其 NAME 属性找到元素。 我尝试了所有可能的方法来在单击元素时触发脚本,并意识到当我使用 - $("input[type=radio]:radio") 并使用类名 - $(".form -control") 我修改了脚本并使用了下面的脚本并且它工作了

$("input[type=radio]:radio").click(function () {
    if ($(this).val() == 'yes') {
        $(".form-control-disable").removeAttr("disabled");
            }
    else if ($(this).val() == 'no') {
        $(".form-control-disable").attr("disabled", "disabled");       
            }
});     

我将以下类用于必须禁用的文本和单选控件“.form-control-disable”并且它起作用了。 我感谢 Abhi 的耐心和帮助。 :)

由于 html 元素是在服务器上呈现的,因此元素的 ID 会发生变化。 在javascript中访问ID时,请使用如下:

$('#<%# chkRelatedStudentYes.ClientID%>').removeAttr("disabled");

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM