我有一张表格,里面有一个带有两个选项的单选按钮(是或否)。之后我有一个下拉列表。我只想在选择"是"时激活列表。单击并以其他方式取消激活。我用JQuery完成了它。但是,它不起作用。不仅列表没有被激活,而且"是"无法检查单选按钮的值!以下是代码:
这是单选按钮:
<input type="radio" name="Married" id="MarriedYes" value="Yes" onclick="hideStudentChildren()" required>YES
<input type="radio" name="Married" id="MarriedNo" value="No" required>NO
这是下拉列表:
<select id="selectChildrenStudent" name="ChildrenStudent" class="required" disabled="true" required>
<option value="" disabled="disabled" selected>--Choose--</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
<option value="6">6</option>
<option value="7">7</option>
<option value="8">8</option>
<option value="9">9</option>
<option value="10">10</option>
</select>
这是JQuery代码:
function hideStudentChildren(){
if ($("#MarriedYes").prop("checked", true)) {
$("#selectChildrenStudent").removeAttr("disabled");
}
if ($("#MarriedYes").prop("checked", false)) {
$("#selectChildrenStudent").attr("disabled", true);
}
}
请任何帮助.....提前谢谢
答案 0 :(得分:1)
你可以做到如下简单......
<input type="radio" name="Married" id="MarriedYes" value="Yes" onclick="hideStudentChildren()" required>YES
<input type="radio" name="Married" id="MarriedNo" value="No" onclick="hideStudentChildren()" required>NO
<script>
function hideStudentChildren(){
if ($("#MarriedYes").is(":checked")) {
$("#selectChildrenStudent").removeAttr("disabled");
}
if ($("#MarriedNo").is(":checked")) {
$("#selectChildrenStudent").attr("disabled","disabled");
}
}
</script>
答案 1 :(得分:0)
在单选按钮中添加相同的课程
<input class="rndMarried" type="radio" name="Married" id="MarriedYes" value="Yes" required>YES
<input class="rndMarried" type="radio" name="Married" id="MarriedNo" value="No" required>NO
$(document).delegate(".rndMarried","change",function(){
//alert($(this).val());
var values=$(this).val();
if(values=="Yes") {
$("#selectChildrenStudent").attr("disabled", true);
}
else {
$("#selectChildrenStudent").attr("disabled", false);
}
});