简体   繁体   English

如何通过使用JQuery对html页面上的先前单选按钮进行某些选择来禁用单选按钮?

[英]How can a radio button be disabled by certain selections of prior radio buttons on an html page using JQuery?

I am developing an HTML application utilizing JQuery and JavaScript that has 5 radio questions with 4 possible answers ("Never", "SomeOfTheTime", "MostOfTheTime", "Always") followed by a Yes/No radio question, like so: 我正在开发一个使用JQuery和JavaScript的HTML应用程序,该应用程序有5个无线电问题,并带有4个可能的答案(“从不”,“ SomeOfTheTime”,“ MostOfTheTime”,“始终”),然后是是/否单选问题,例如:

radio1 (id="mobility") 1 2 3 4 radio1(id =“ mobility”)1 2 3 4

radio2 (id="feeding") 1 2 3 4 radio2(id =“ feeding”)1 2 3 4

radio3 (id="washingDressing") 1 2 3 4 radio3(id =“ washingDressing”)1 2 3 4

radio4 (id="bladder") 1 2 3 4 radio4(id =“ bladder”)1 2 3 4

radio5 (id="bowel") 1 2 3 4 radio5(id =“ bowel”)1 2 3 4

radio6 (id="moreAssistance") YN radio6(id =“ moreAssistance”)YN

I am trying to make it such that if the answer to ALL 5 of radios 1-5 are the first option (value) of "Never", then radio 6 is checked No and disabled (else radio 6 is enabled with no option checked), but I can't get it to work. 我试图这样做,如果对无线电1-5的全部5个的回答是“从不”的第一个选项(值),则无线电6被选中“否”并被禁用(其他无线电6被启用而未选中任何选项) ,但我无法正常工作。

Here is the HTML for the first radio; 这是第一个电台的HTML。 the others are similar: 其他类似:

<tr>
<td><label for="mobility">Mobility:<span style="color:red">*</span></label></td>
<td><form:radiobutton path="mobility" id="mobility" value="Never" required="required" /></td>
<td><form:radiobutton path="mobility" id="mobility" value="SomeOfTheTime" required="required" /></td>
<td><form:radiobutton path="mobility" id="mobility" value="MostOfTheTime" required="required" /></td>
<td><form:radiobutton path="mobility" id="mobility" value="Always" required="required" /></td>

I wrote the JQuery scripts, one for each of the 5 radios, the same for all except for the first line with the change function substituting the next radio id, but they don't seem to do the trick. 我编写了JQuery脚本,每个5个无线电都一个,除了第一行用change函数替换下一个无线电ID之外,其余所有脚本都一样,但是它们似乎并没有解决问题。 I am displaying the first one below: 我在下面显示第一个:

$("#mobility").change(function() {

    if ($("#mobility:radio[value='Never']").prop("checked") && $("#feeding:radio[value='Never']").prop("checked") && $("#washingdressing:radio[value='Never']").prop("checked") && $("#bladder:radio[value='Never']").prop("checked") && $("#bowel:radio[value='Never']").prop("checked") ) {

        $("#moreAssistance:radio[value='No']").prop("checked");
        $("#moreAssistance input[type=radio]").attr('disabled', 'disabled');

    } else {
        $("#moreAssistance input[type=radio]").attr('disabled', false);
        $("#moreAssistance:radio[value='No']").prop(checked, false);
    }
}); 

Then I tried to do it by the following code in an attempt to make it more succinct, by trying to do the value check on all 5 of the top radios at once, but that didn't work either: 然后,我尝试通过下面的代码来做到这一点,以试图使其更加简洁,即尝试一次对所有5个顶级收音机进行值检查,但这都不起作用:

$(":radio").not("moreAssistance").change(function() {

    if ($(":radio").not("moreAssistance").val==="Never") {

        $("#moreAssistance:radio[value='No']").prop("checked");
        $("#moreAssistance input[type=radio]").attr('disabled', 'disabled');

    } else {

        $("#moreAssistance input[type=radio]").attr('disabled', false);
        $("#moreAssistance:radio[value='No']").prop(checked, false);

    }
});

Can anyone help me to get this to work? 谁能帮我使它正常工作? I apologize for my HTML/JQuery ineptness - this is my first web project using JQuery and JavaScript and I am a Newbie to both. 我为我的HTML / JQuery不足而道歉-这是我的第一个使用JQuery和JavaScript的Web项目,我俩都是新手。 I appreciate any help I can get! 感谢您能提供的任何帮助!

A quick and dirty HTML for demonstration: 快速,肮脏的HTML演示:

1 <input type="radio" name="mobility" value="never"> <input type="radio" name="mobility" value="some"> <input type="radio" name="mobility" value="most"> <input type="radio" name="mobility" value="always"><br>
2 <input type="radio" name="feeding" value="never"> <input type="radio" name="feeding" value="some"> <input type="radio" name="feeding" value="most"> <input type="radio" name="feeding" value="always"><br>
3 <input type="radio" name="washing" value="never"> <input type="radio" name="washing" value="some"> <input type="radio" name="washing" value="most"> <input type="radio" name="washing" value="always"><br>
4 <input type="radio" name="bladder" value="never"> <input type="radio" name="bladder" value="some"> <input type="radio" name="bladder" value="most"> <input type="radio" name="bladder" value="always"><br>
5 <input type="radio" name="bowel" value="never"> <input type="radio" name="bowel" value="some"> <input type="radio" name="bowel" value="most"> <input type="radio" name="bowel" value="always"><br>
6 <input type="radio" name="assistance" value="Y"><input type="radio" name="assistance" value="N">

JavaScript: JavaScript:

<script>
$(':radio:not([name=assistance])').change(function(){
    var total = $(':radio[value=never]:checked').length;
    if (total === 5) {
        $('input[name=assistance]').prop('disabled', true);
        $('input[name=assistance][value=N]').prop('checked', true);
    }
    else {
        $('input[name=assistance]').prop('disabled', false);
    }
});
</script>

Demo on JSBin JSBin 演示

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

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