如何使用单选按钮并显示此表单

时间:2018-03-08 23:51:57

标签: javascript html5 materialize

我正在使用materialize框架,我必须使用java脚本填充此表单。我不知道如何在我的js文件中访问它们

<div class="row col s10 offset-s1" id="questionArea" style="display:none">
      <blockquote class="flow-text"> Select the capital of the country shown below.</blockquote>
     <div class="input-field col s6">
       Question <span id="quesTextSpan"></span><p>
         <input name="qOptions" type="radio" id="optionId0" class="with-gap qOptionClass" />
         <label for="optionId0"></label>
       </p>
       <p>
         <input name="qOptions" type="radio" id="optionId1" class="with-gap qOptionClass" />
         <label for="optionId1"></label>
       </p>
       <p>
         <input  name="qOptions" type="radio" id="optionId2" class="with-gap qOptionClass" />
         <label for="optionId2">d</label>
       </p>
       <p>
         <input  name="qOptions" type="radio" id="optionId3" class="with-gap qOptionClass" />
         <label for="optionId3"></label>
       </p>
     </div>
    </div>

1 个答案:

答案 0 :(得分:-1)

您可以像这样访问单选按钮的值:

var radioVal = document.getElementById('optionId0').checked;

然后,您可以使用条件和函数对它们执行逻辑。

为了快速收集它们,我建议你这样做。

var radios = document.querySelectorAll('input[type="radio"]');

这将为您提供包含每个元素的数组。 然后,您可以通过并将值更改为true或false,具体取决于您希望如何填充它们。

实施例)

radios[0].checked = true // Sets the first radio button to be on.

编辑)

显示div使用:

document.getElementById('questionArea').style.display = ''; 

隐藏div使用:

document.getElementById('questionArea').style.display = 'hidden';
相关问题