javascript - 使用 jquery 启用/禁用对 radio 选择的控制

标签 javascript jquery asp.net visibility disabled-control

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

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/

关于javascript - 使用 jquery 启用/禁用对 radio 选择的控制,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36235815/

相关文章:

c# - 改进 IIS .NET MVC 站点中 DLL 更改后的首次请求时间

c# - ASP.NET MVC - 如何调用 Controller 方法并且不离开 View

javascript结合多个正则表达式

javascript - 如何检查表单中的文本输入是否仅包含数字和特定前缀?

javascript - JQuery:选中/取消选中单选按钮

使用 jQuery 进行 ASP.NET 回发?

c# - FCKeditor 在 asp.net 中的 url 重写有问题?

javascript - 一个 redux 操作即可管理多个项目

javascript - WebRTC远程录音解决方案

javascript - 使用 jQuery 从 json 动态呈现无序列表