【问题标题】:select tag is not working in dynamic table for multiple row选择标签在多行的动态表中不起作用
【发布时间】:2020-10-13 15:50:34
【问题描述】:

在我的代码中动态添加带有一些输入类型数字、文本字段和选择字段的表格行 并总共打印输出..所以我的问题是输出仅打印第一行它没有为其他行提供输出...当我删除选择字段时它对剩余字段工作正常..所以我有什么变化必须这样做,我的代码也适用于 selectfield 吗?

<HTML>
<HEAD>
    <TITLE> Add/Remove dynamic rows in HTML table </TITLE>
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">

    <SCRIPT language="javascript">
function addRow(tableID) {
    var table = document.getElementById(tableID);
    var rowCount = table.rows.length;
    if (rowCount < 4) { // limit the user from creating fields more than your limits
        var row = table.insertRow(rowCount);

        var colCount = table.rows[0].cells.length;
        row.id = 'row_'+rowCount;
        for (var i = 0; i < colCount; i++) {
            var newcell = row.insertCell(i);
            newcell.outerHTML = table.rows[0].cells[i].outerHTML;            
        }
       var listitems= row.getElementsByTagName("input")
            for (i=0; i<listitems.length; i++) {
              listitems[i].setAttribute("oninput", "calculate('"+row.id+"')");
            }
    } else {
        alert("Maximum Passenger per ticket is 4.");

    }
}



function calculate(elementID) {
    var mainRow = document.getElementById(elementID);
    var myBox1 = mainRow.querySelectorAll('[name=qty]')[0].value;
    var myBox2 = mainRow.querySelectorAll('[name=price]')[0].value;
    var myBox3 = mainRow.querySelectorAll('[name^=sel]')[0].value;

    var total = mainRow.querySelectorAll('[name=total]')[0];
    var myResult1 = myBox1 * myBox2 * myBox3;
    total.value = myResult1;

}    </SCRIPT>
</HEAD>
<BODY>
    <script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js" integrity="sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js" integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous"></script> 
   <input type="button" value="Add" onClick="addRow('dataTable')" />

<table id="dataTable" class="form" border="1">
    <tbody>
        <tr id='row_0'>
            <p>
                <td>
                    <label>Quantity</label>
                    <input type="number" required="required" name="qty" oninput="calculate('row_0')">
                </td>
                <td>
                    <label for="price">Price</label>
                    <input type="text" required="required" class="small" name="price" oninput="calculate('row_0')">
                </td>
                <td>
                     <label for="sel">Price</label>                 
                    <select name="sel" id="sel" oninput="calculate('row_0')" required>
            <option value="" disabled selected>Choose your option</option>
            <option value="1">1</option>
            <option value="2">2</option>
            <option value="3">3</option>
            <option value="4">4</option>
            <option value="5">5</option>
            <option value="6">6</option>
            <option value="7">7</option>
            <option value="8">8</option>
            <option value="9">9</option>
            <option value="10">10</option>
        </select>
                </td>
                <td>
                    <label for="total">Total</label>
                    <input type="text" required="required" class="small" name="total">
                </td>
            </p>
        </tr>
    </tbody>
</table>
</BODY>
</HTML>

【问题讨论】:

    标签: javascript html


    【解决方案1】:

    你做错了什么?

    • 您忘记选择了,还有inputselect

    我改变了什么?

    • 诀窍就在这里更改row.querySelectorAll("input, select"); 而不是row.getElementsByTagName("input")

    <HTML>
    <HEAD>
        <TITLE> Add/Remove dynamic rows in HTML table </TITLE>
        <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
    
        <SCRIPT language="javascript">
    function addRow(tableID) {
        var table = document.getElementById(tableID);
        var rowCount = table.rows.length;
        if (rowCount < 4) { // limit the user from creating fields more than your limits
            var row = table.insertRow(rowCount);
    
            var colCount = table.rows[0].cells.length;
            row.id = 'row_'+rowCount;
            for (var i = 0; i < colCount; i++) {
                var newcell = row.insertCell(i);
                newcell.outerHTML = table.rows[0].cells[i].outerHTML;            
            }
           var listitems= row.querySelectorAll("input, select");
           
                for (i=0; i<listitems.length; i++) {
                  listitems[i].setAttribute("oninput", "calculate('"+row.id+"')");
                }
              
        } else {
            alert("Maximum Passenger per ticket is 4.");
    
        }
    }
    
    
    
    function calculate(elementID) {
        var mainRow = document.getElementById(elementID);
        var myBox1 = mainRow.querySelectorAll('[name=qty]')[0].value;
        var myBox2 = mainRow.querySelectorAll('[name=price]')[0].value;
        var myBox3 = mainRow.querySelectorAll('[name^=sel]')[0].value;
        var total = mainRow.querySelectorAll('[name=total]')[0];
        var myResult1 = myBox1 * myBox2 * myBox3;
        total.value = myResult1;
    
    }    </SCRIPT>
    </HEAD>
    <BODY>
        <script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js" integrity="sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1" crossorigin="anonymous"></script>
    <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js" integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous"></script> 
       <input type="button" value="Add" onClick="addRow('dataTable')" />
    
    <table id="dataTable" class="form" border="1">
        <tbody>
            <tr id='row_0'>
                <p>
                    <td>
                        <label>Quantity</label>
                        <input type="number" required="required" name="qty" oninput="calculate('row_0')">
                    </td>
                    <td>
                        <label for="price">Price</label>
                        <input type="text" required="required" class="small" name="price" oninput="calculate('row_0')">
                    </td>
                    <td>
                         <label for="sel">Price</label>                 
                        <select name="sel" id="sel" oninput="calculate('row_0')" required>
                <option value="" disabled selected>Choose your option</option>
                <option value="1">1</option>
                <option value="2">2</option>
                <option value="3">3</option>
                <option value="4">4</option>
                <option value="5">5</option>
                <option value="6">6</option>
                <option value="7">7</option>
                <option value="8">8</option>
                <option value="9">9</option>
                <option value="10">10</option>
            </select>
                    </td>
                    <td>
                        <label for="total">Total</label>
                        <input type="text" required="required" class="small" name="total">
                    </td>
                </p>
            </tr>
        </tbody>
    </table>
    </BODY>
    </HTML>

    【讨论】:

      【解决方案2】:

      在这里改变

      function calculate(tableID) { // elementID to tableID
         var mainRow = document.getElementById(tableID); // elementID to tableID
      }
      

      设置选择框

      setAttribute("oninput", "calculate('"+row.id+"')");
      

      <HTML>
      <HEAD>
          <TITLE> Add/Remove dynamic rows in HTML table </TITLE>
          <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
      
          <SCRIPT language="javascript">
      function addRow(tableID) {
                  var table = document.getElementById(tableID);
                  var rowCount = table.rows.length;
                  if (rowCount < 4) { // limit the user from creating fields more than your limits
                      var row = table.insertRow(rowCount);
      
                      var colCount = table.rows[0].cells.length;
                      row.id = 'row_' + rowCount;
                      for (var i = 0; i < colCount; i++) {
                          var newcell = row.insertCell(i);
                          newcell.outerHTML = table.rows[0].cells[i].outerHTML;
                      }
                      var listitems = row.getElementsByTagName("input")
                      for (i = 0; i < listitems.length; i++) {
                          listitems[i].setAttribute("onchange", "calculate('" + row.id + "')");
      
      
                      }
                      var selectbox = row.getElementsByTagName("select")
                      selectbox[0].setAttribute("onchange", "calculate('" + row.id + "')");
                  } else {
                      alert("Maximum Passenger per ticket is 4.");
      
                  }
              }
      
      
      
              function calculate(tableID) {
                  debugger;
                  var mainRow = document.getElementById(tableID);
                  var myBox1 = mainRow.querySelectorAll('[name=qty]')[0].value;
                  var myBox2 = mainRow.querySelectorAll('[name=price]')[0].value;
                  var myBox3 = mainRow.querySelectorAll('[name=sel]')[0].value;
      
                  var total = mainRow.querySelectorAll('[name=total]')[0];
                  var myResult1 = myBox1 * myBox2 * myBox3;
                  total.value = myResult1;
      
              } </SCRIPT>
      </HEAD>
      <BODY>
          <script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
      <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js" integrity="sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1" crossorigin="anonymous"></script>
      <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js" integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous"></script> 
         <input type="button" value="Add" onClick="addRow('dataTable')" />
      
      <table id="dataTable" class="form" border="1">
          <tbody>
              <tr id='row_0'>
                  <p>
                      <td>
                          <label>Quantity</label>
                          <input type="number" required="required" name="qty" oninput="calculate('row_0')">
                      </td>
                      <td>
                          <label for="price">Price</label>
                          <input type="text" required="required" class="small" name="price" oninput="calculate('row_0')">
                      </td>
                      <td>
                           <label for="sel">Price</label>                 
                          <select name="sel" id="sel" oninput="calculate('row_0')" required>
                  <option value="" disabled selected>Choose your option</option>
                  <option value="1">1</option>
                  <option value="2">2</option>
                  <option value="3">3</option>
                  <option value="4">4</option>
                  <option value="5">5</option>
                  <option value="6">6</option>
                  <option value="7">7</option>
                  <option value="8">8</option>
                  <option value="9">9</option>
                  <option value="10">10</option>
              </select>
                      </td>
                      <td>
                          <label for="total">Total</label>
                          <input type="text" required="required" class="small" name="total">
                      </td>
                  </p>
              </tr>
          </tbody>
      </table>
      </BODY>
      </HTML>

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-04-29
        • 1970-01-01
        • 1970-01-01
        • 2014-08-30
        • 2015-10-10
        相关资源
        最近更新 更多