我正在尝试启用/禁用某些控件,具体取决于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>
答案 0 :(得分:1)
您的触发事件的方式是错误的
请尝试这个,这里是完整的代码,应该有效
注意:经过测试
$(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");
}
});
});
尝试
答案 1 :(得分:0)
您可以使用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);
});
答案 2 :(得分:0)
我发现了问题所在。 该脚本无法按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的耐心和帮助。 :)
答案 3 :(得分:0)
由于html元素在服务器上呈现,因此元素的ID将发生变化。 在javascript中访问ID时,请使用以下内容:
$('#<%# chkRelatedStudentYes.ClientID%>').removeAttr("disabled");