【问题标题】:Adding radio input dynamically doesn't work with others with the same name动态添加无线电输入不适用于同名的其他人
【发布时间】:2015-11-07 23:32:29
【问题描述】:

想象一下这个问题:我有一个带有 3 个同名单选输入的表单,我有一个按钮,它添加了另一个与前 3 个同名的单选输入。问题是,在我通过添加另一个单选字段之后按钮收音机不与前 3 台收音机合作(就像它为这些收音机添加了另一组)。

要查看问题,请按几次“添加线路”按钮,而不是使用单选按钮。前 3 个默认收音机在一个组中,添加的一个在不同组中。

谁能告诉我如何让所有这些收音机相互配合(就像在一个组中一样)?

代码:

function addAnswer(value) {
  thebutton = value;
  console.log(value);
  table = document.getElementById("thetable");
  row = table.insertRow(table.rows.length);

  cell1 = row.insertCell(0);
  cell2 = row.insertCell(1);
  cell1.innerHTML = '<input type="text" name="answer[]" required style="width: 100%;" placeholder="Wpisz tutaj odpowiedź">';
  cell2.innerHTML = '<input type="radio" value="' + value + '" name="correct">';

  document.getElementById("addbutton").value = parseInt(value) + 1;

}
<table style="width: 100%;" id="thetable">
  <form action="" method="POST">
    <tr>
      <td style="width: 80%;">Odpowiedź</td>
      <td style="width: 20%;">Poprawna</td>
    </tr>
    <tr>
      <td>
        <input type="text" name="answer[]" required style="width: 100%;" placeholder="Wpisz tutaj odpowiedź">
      </td>
      <td>
        <input type="radio" id="radio0" name="correct" value="0">
      </td>
    </tr>
    <tr>
      <td>
        <input type="text" name="answer[]" required style="width: 100%;" placeholder="Wpisz tutaj odpowiedź">
      </td>
      <td>
        <input type="radio" id="radio1" name="correct" value="1">
      </td>
    </tr>
    <tr>
      <td>
        <input type="text" name="answer[]" required style="width: 100%;" placeholder="Wpisz tutaj odpowiedź">
      </td>
      <td>
        <input type="radio" id="radio2" name="correct" value="2">
      </td>
    </tr>

</table>
<button type="button" value="3" id="addbutton" onclick="addAnswer(this.value)" style="width: auto; height: auto;">Add line</button>
<input type="hidden" name="questionId" value="<?php echo $id[0]['id']; ?>">
</form>

【问题讨论】:

    标签: javascript html dynamic radio-button


    【解决方案1】:

    如果你切换这两行,表格就在表格里面。也许这会解决你的问题:

    <table style="width: 100%;" id="thetable">
      <form action="" method="POST">
    

    function addAnswer(value) {
      thebutton = value;
      console.log(value);
      table = document.getElementById("thetable");
      row = table.insertRow(table.rows.length);
    
      cell1 = row.insertCell(0);
      cell2 = row.insertCell(1);
      cell1.innerHTML = '<input type="text" name="answer[]" required style="width: 100%;" placeholder="Wpisz tutaj odpowiedź">';
      cell2.innerHTML = '<input type="radio" value="' + value + '" name="correct">';
    
      document.getElementById("addbutton").value = parseInt(value) + 1;
    
    }
    <form action="" method="POST">
        <table style="width: 100%;" id="thetable">
        <tr>
          <td style="width: 80%;">Odpowiedź</td>
          <td style="width: 20%;">Poprawna</td>
        </tr>
        <tr>
          <td>
            <input type="text" name="answer[]" required style="width: 100%;" placeholder="Wpisz tutaj odpowiedź">
          </td>
          <td>
            <input type="radio" id="radio0" name="correct" value="0">
          </td>
        </tr>
        <tr>
          <td>
            <input type="text" name="answer[]" required style="width: 100%;" placeholder="Wpisz tutaj odpowiedź">
          </td>
          <td>
            <input type="radio" id="radio1" name="correct" value="1">
          </td>
        </tr>
        <tr>
          <td>
            <input type="text" name="answer[]" required style="width: 100%;" placeholder="Wpisz tutaj odpowiedź">
          </td>
          <td>
            <input type="radio" id="radio2" name="correct" value="2">
          </td>
        </tr>
    
    </table>
    <button type="button" value="3" id="addbutton" onclick="addAnswer(this.value)" style="width: auto; height: auto;">Add line</button>
    <input type="hidden" name="questionId" value="<?php echo $id[0]['id']; ?>">
    </form>

    【讨论】:

    • 天哪,这么愚蠢的错误让我的工作停了 2 个小时。您的解决方案是正确的,谢谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-03
    • 1970-01-01
    • 2016-01-27
    • 1970-01-01
    • 2021-05-21
    • 2014-12-30
    相关资源
    最近更新 更多