首页 文章

如何将单选按钮值与输入字段关联

提问于
浏览
1

我正在创建一个测验应用程序......

我创建了一个问题文本区域,以及许多选项的输入字段 .

每个输入字段都有一个单选按钮,如果用户单击它,它会将相关的输入字段视为正确的答案 .

enter image description here

我已经为每个输入字段创建了一些输入字段和单选按钮

<div id="items">
    <input class="form-control" type="text" name="options[]" required /> Is Correct: <input type="radio" name="is_correct" value="yes"/> <br><br>
    <input class="form-control" type="text" name="options[]" required /> Is Correct: <input type="radio" name="is_correct" value="yes"/>
</div>

当我提交此表单时,它会为每个选项存储 yes . 我无法理解如何存储仅1个输入字段的值 .

请指导我谢谢 .

2 回答

  • 2

    您可以使用以下输入:

    <div id="items">
        <input class="form-control" type="text" name="options[]" required /> Is Correct: <input type="radio" name="is_correct[]" value="yes"/> <br><br>
        <input class="form-control" type="text" name="options[]" required /> Is Correct: <input type="radio" name="is_correct[]" value="yes"/>
    </div>
    
  • 2

    如果获得选项的值,则input1 answer为input1,或者如果options值为input2,则answer为input2

    <input type="radio" name="options" value="input1">Is Correct<br>
    <input type="radio" name="options" value="input2">Is Correct
    

    input1是文本框的名称1 input2是文本框2的名称

    在表格上获取 Value : -

    $nam=$_POST['options'];
    $answer=$_POST[$nam];
    

相关问题