"关闭"在回答先前输入时,指定输入中的必需属性

时间:2015-05-27 02:59:46

标签: html

我希望在回答特定的首要问题时关闭一些问题的表单验证。例如,如果问题5被回答为" no"则不再需要问题6-10。

HTML代码段位于上下文中。

<ul class="ss-choices" role="group" aria-label="No Mobile Site  "><li class="ss-choice-item"><label><span class="ss-choice-item-control goog-inline-block">
<input type="checkbox" name="entry.1999905937" value="" id="group_1999905937_1" role="checkbox" class="ss-q-checkbox"></span>

<td class="ss-scalerow">
<div class="ss-scalerow-fieldcell">
<input type="radio" name="entry.131573405" value="1" id="group_131573405_1" role="radio" class="ss-q-radio" aria-label="1" required="" aria-required="true">
</div></td> 
<td class="ss-scalerow"><div class="ss-scalerow-fieldcell">
<input type="radio" name="entry.131573405" value="2" id="group_131573405_2" role="radio" class="ss-q-radio" aria-label="2" required="" aria-required="true"></div></td> <td class="ss-scalerow"><div class="ss-scalerow-fieldcell">
<input type="radio" name="entry.131573405" value="3" id="group_131573405_3" role="radio" class="ss-q-radio" aria-label="3" required="" aria-required="true"></div></td> <td class="ss-scalerow"><div class="ss-scalerow-fieldcell">
<input type="radio" name="entry.131573405" value="4" id="group_131573405_4" role="radio" class="ss-q-radio" aria-label="4" required="" aria-required="true"></div></td> <td class="ss-scalerow"><div class="ss-scalerow-fieldcell">
<input type="radio" name="entry.131573405" value="5" id="group_131573405_5" role="radio" class="ss-q-radio" aria-label="5" required="" aria-required="true"></div>  

如果选中了复选框输入,我希望能够禁用所有无线电输入的必需属性。

1 个答案:

答案 0 :(得分:2)

单击复选框时,您需要设置并清除所需的属性

(function(document){
    var checkbox = document.getElementById('checkbox');
    var radio = document.getElementsByClassName('radio-buttons');

    var onCheckboxClick = function(){
        if(this.checked){
            radio.forEach(function(r){
                r.removeAttribute('required');
            });
        } else{
            radio.forEach(function(r){
                r.setAttribute('required', '');
            });
        }
    };

    checkbox.addEventListener('click', onCheckboxClick);
})(document);