启用/禁用使用jquery选择无线电的控制

Enable/Disable Controls on selection of Radio using jquery

本文关键字:无线电 控制 选择 启用 jquery      更新时间:2023-09-26

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

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-pro()方法而不是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",它起作用了。我感谢阿布希的耐心和帮助

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

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