0

我有两组复选框,当我单击第一组复选框时,应取消选中第二组复选框。但是当我两次单击第一个复选框时,第二个复选框未选中。这是我的 javascript 代码

 <script>
            function uncheck0(){
                for(var ii=1; ii<=3; ii++){
                    if(document.getElementById("q6_"+ii).checked==true){
                       document.getElementById("q6_"+ii).checked=false;
                    }
                }
            }
            function uncheck(id, from, to){
                for(var ii=from; ii<=to; ii++){
                    if(document.getElementById(id+ii).checked==true){
                       document.getElementById(id+ii).checked=false;
                    }
                }
            }
        </script>

这是我的html 代码,第一组有单选按钮,第二组有复选框

<p>IF YES: When will the review begin?</p>
            <label  for="q6"><input type="radio" class="styled" value="Within the next 12 months" name="q6[]" id="q6_1">Within the next 12 months</label>
            <label  for="q6"><input type="radio" class="styled" value="Within the next 24 months" name="q6[]" id="q6_2">Within the next 24 months</label>
            <label  for="q6"><input type="radio" class="styled" value="We are currently doing it" name="q6[]" id="q6_3">We are currently doing it</label><br>
        <br>
        <p>IF NO: Why Not</p>
            <label onclick="uncheck('q6_',1,3);" for="q6"><input type="checkbox" class="styled" value="Lack of budget" name="q6[]" id="q6_4">Lack of budget</label>
            <label onclick="uncheck('q6_',1,3);" for="q6"><input type="checkbox" class="styled" value="Lack of resource" name="q6[]" id="q6_5">Lack of resource</label>
            <label onclick="uncheck('q6_',1,3);" for="q6"><input type="checkbox" class="styled" value="Do not see the need" name="q6[]" id="q6_6">Do not see the need</label>
            <label onclick="uncheck('q6_',1,3);" for="q6"><input type="checkbox" class="styled" value="Lack of know how/expertise" name="q6[]" id="q6_7">Lack of know how/expertise</label>
            <label onclick="uncheck('q6_',1,3);" for="q6"><input type="checkbox" class="styled" value="Complexity" name="q6[]" id="q6_8">Complexity</label>
            <label onclick="uncheck('q6_',1,3);" for="q6"><input type="checkbox" class="styled" value="Contractual obligations" name="q6[]" id="q6_9">Contractual obligations</label>
            <label onclick="uncheck('q6_',1,3);" for="q5"><input type="checkbox" class="styled" value="Other" name="q6[]" id="q6_10">Other </label>
            <label onclick="uncheck('q6_',1,3);" for="q5a">Please state</label><input type="text" value="" id="q6a" name="q6a">

我已使用 javascript 文件“custom-form-element.js”进行复选框和单选样式

4

2 回答 2

2

您需要有一个默认的“检查”值

 <input type="checkbox" checked="true">

否则,您的代码会认为它未选中,然后将值设置为“选中”,然后第二次单击将“取消选中”它。

于 2012-06-08T07:02:36.687 回答
2

onclick="uncheck('q6_',1,3);"标签从标签移到输入框。
您的最终代码应类似于:

<label for="q6">
    <input type="checkbox" onclick="uncheck('q6',1,3);" class="styled" value="Lack of budget" name="q6[]" id="q6_1">Lack of budget</label>
<label onclick="uncheck('q6_',1,3);" for="q6_2"><input type="checkbox" class="styled" value="Lack of resource" name="q6[]" id="q6">Lack of resource</label>
于 2012-06-08T07:02:45.237 回答