如何使用 HTML 强制用户 select 一个复选框?
How o force the user to select a single checkbox with HTML?
我正在 HTML 创建一个测验。可以通过 select 选中复选框来回答问卷。到目前为止试试这个:
<h3>This is question 1?</h3>
<input type="radio" name="answer1" id="c01" value="0"/> answer A
<input type="radio" name="answer2" id="c02" value="0" /> answer B
<h3>This is question 2?</h3>
<input type="radio" name="answer3" id="c03" value="0"/> answer 1
<input type="radio" name="answer4" id="c04" value="0" /> answer 2
<input type="radio" name="answer5" id="c05" value="0"/> answer 3
但是,前面的陈述不起作用,因为我只想 select 一个且只有一个答案(select不允许每个答案有一个以上的复选框)。我如何通过仅使用 html 标签强制用户对每个不同的问题仅 select 一个复选框?
您需要为所有 input type="radio" 赋予相同的名称,这会将它们变成单选按钮,而不是像现在本质上那样的复选框。
谢谢,
使用字段集:
<fieldset id="question1">
<legend>Question 1:</legend>
<input type="radio" value="value1" name="question1">Answer 1</input>
<input type="radio" value="value2" name="question1">Answer 2</input>
</fieldset>
<fieldset id="question2">
<legend>Question 2:</legend>
<input type="radio" value="value1" name="question2">Answer 1</input>
<input type="radio" value="value2" name="question2">Answer 2</input>
</fieldset>
没有字段集:
<h3>This is question 1?</h3>
<input type="radio" value="value1" name="question1">Answer 1</input>
<input type="radio" value="value2" name="question1">Answer 2</input>
<h3>This is question 2?</h3>
<input type="radio" value="value1" name="question2">Answer 1</input>
<input type="radio" value="value2" name="question2">Answer 2</input>
我正在 HTML 创建一个测验。可以通过 select 选中复选框来回答问卷。到目前为止试试这个:
<h3>This is question 1?</h3>
<input type="radio" name="answer1" id="c01" value="0"/> answer A
<input type="radio" name="answer2" id="c02" value="0" /> answer B
<h3>This is question 2?</h3>
<input type="radio" name="answer3" id="c03" value="0"/> answer 1
<input type="radio" name="answer4" id="c04" value="0" /> answer 2
<input type="radio" name="answer5" id="c05" value="0"/> answer 3
但是,前面的陈述不起作用,因为我只想 select 一个且只有一个答案(select不允许每个答案有一个以上的复选框)。我如何通过仅使用 html 标签强制用户对每个不同的问题仅 select 一个复选框?
您需要为所有 input type="radio" 赋予相同的名称,这会将它们变成单选按钮,而不是像现在本质上那样的复选框。
谢谢,
使用字段集:
<fieldset id="question1">
<legend>Question 1:</legend>
<input type="radio" value="value1" name="question1">Answer 1</input>
<input type="radio" value="value2" name="question1">Answer 2</input>
</fieldset>
<fieldset id="question2">
<legend>Question 2:</legend>
<input type="radio" value="value1" name="question2">Answer 1</input>
<input type="radio" value="value2" name="question2">Answer 2</input>
</fieldset>
没有字段集:
<h3>This is question 1?</h3>
<input type="radio" value="value1" name="question1">Answer 1</input>
<input type="radio" value="value2" name="question1">Answer 2</input>
<h3>This is question 2?</h3>
<input type="radio" value="value1" name="question2">Answer 1</input>
<input type="radio" value="value2" name="question2">Answer 2</input>