weixin_33714884 2019-11-25 10:51 采纳率: 0%
浏览 23

非依赖保管箱

I have the following code:

function addRow(tableID) {
  var table = document.getElementById(tableID);
  var rowCount = table.rows.length;
  var row = table.insertRow(rowCount);
  var colCount = table.rows[0].cells.length;

  for (var i = 0; i < colCount; i++) {
    var newcell = row.insertCell(i);
    newcell.style.property = 'value'
    newcell.innerHTML = table.rows[0].cells[i].innerHTML;
    //alert(newcell.childNodes);
    switch (newcell.childNodes[0].type) {
      case "text":
        newcell.childNodes[0].value = "";
        break;
      case "checkbox":
        newcell.childNodes[0].checked = false;
        break;
      case "select-one":
        newcell.childNodes[0].selectedIndex = 0;
        break;
    }
  }
}


function showUser(str) {
  if (str == "") {
    document.getElementById("txtHint").innerHTML = "";
    return;
  } else {
    if (window.XMLHttpRequest) {
      // code for IE7+, Firefox, Chrome, Opera, Safari
      xmlhttp = new XMLHttpRequest();
    } else {
      // code for IE6, IE5
      xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    }
    xmlhttp.onreadystatechange = function() {
      if (this.readyState == 4 && this.status == 200) {
        document.getElementById("txtHint").innerHTML = this.responseText;
      }
    };
    xmlhttp.open("GET", "getcategory.php?q=" + str, true);
    xmlhttp.send();
  }
}
<form action="edit.php" method="post">
  <table cellpadding="0" cellspacing="0" border="0" class="table table-striped table-bordered" id="dataTable">
    <tr>
      <td>
        <select name="id" id="id" width="100%" onchange="showUser(this.value)">
          <option value="">SELECT A TOPIC</option>
          <option value="1">1</option>
          <option value="2">2</option>
          <option value="3">3</option>
        </select>
      </td>
      <td>
        <div id="txtHint"><b>Select a topic...</b></div>
      </td>
      <td>aaa</td>
    </tr>
  </table>
  <br />
  <input type="button" class="btn btn-warning pull-left" value="Add another topic" onclick="addRow('dataTable')" />
  <button class="btn btn-success pull-right" name="submit_mult" type="submit" style="size:50;">
        Start Quiz
    </button>
</form>

code.php

<select name="id" id="id" width="100%" onchange="showUser(this.value)">
<option value="">SELECT A TOPIC</option>    
<?php 
echo "<option value=".$id.">".$row['category_name']."</option>";
?>
<div id="txtHint"><b>Select a topic...</b></div>
<button type="button" class="btn btn-warning pull-left" value="" onclick="addRow('dataTable')" />   
    Add another topic
</button>
<button class="btn btn-success pull-right" name="submit_mult" type="submit" style="size:50;">
    Start Quiz
</button>

script.js

function addRow(tableID) {

    var table = document.getElementById(tableID);

    var rowCount = table.rows.length;
    var row = table.insertRow(rowCount);

    var colCount = table.rows[0].cells.length;

    for(var i=0; i<colCount; i++) {

        var newcell = row.insertCell(i);
        newcell.style.property='value'

        newcell.innerHTML = table.rows[0].cells[i].innerHTML;

        //alert(newcell.childNodes);
        switch(newcell.childNodes[0].type) {
            case "text":
                    newcell.childNodes[0].value = "";
                    break;
            case "checkbox":
                    newcell.childNodes[0].checked = false;
                    break;
            case "select-one":
                    newcell.childNodes[0].selectedIndex = 0;
                    break;
        }
    }
}


function showUser(str) {
    if (str == "") {
        document.getElementById("txtHint").innerHTML = "";
        return;
    } else {
        if (window.XMLHttpRequest) {
            // code for IE7+, Firefox, Chrome, Opera, Safari
            xmlhttp = new XMLHttpRequest();
        } else {
            // code for IE6, IE5
            xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
        }
        xmlhttp.onreadystatechange = function() {
            if (this.readyState == 4 && this.status == 200) {
                document.getElementById("txtHint").innerHTML = this.responseText;
            }
        };
        xmlhttp.open("GET","getcategory.php?q="+str,true);
        xmlhttp.send();
    }
}

So, the thing is, whenever I add another row in the table clicking in the button "add another topic" and select an option in the dropbox of that row, instead of changing the data of that row it changes the data of the first row.

enter image description here In this example, I changed the selection of the dropbox menu of the 5th row, but it will always only affect the 1st row, as you can see.

Can anyone help me with that, please?

</div>
  • 写回答

1条回答 默认 最新

  • weixin_33709219 2019-11-25 13:32
    关注

    I think you wanted to do this.

    I cannot test the Ajax but uncomment and try

    function addRow(tableID) {
      var table = document.getElementById(tableID);
      var rowCount = table.rows.length;
      var newRow = table.rows[0].cloneNode(true);
      newRow.querySelector(".txtHint").innerHTML="<b>Select a topic</b>";
      [...newRow.querySelectorAll("input, select")].forEach(inp => {
        switch (inp.type) {
          case "text":
            inp.value = "";
            break;
          case "checkbox":
            inp.checked = false;
            break;
          case "select-one":
            inp.id = "sel" + rowCount;
            inp.selectedIndex = 0;
            break;
        }
      })
      table.appendChild(newRow);
    
    }
    
    
    function showUser(sel) {
      var str = sel.value,
        id = sel.id,
        sel = document.getElementById(id);
      if (str == "") {
        var rowCount = table.rows.length;
        sel.closest("tr").querySelector(".textHint").innerHTML = "";
        return;
      } else {
        console.log(id, str)
        /*
          if (window.XMLHttpRequest) {
            // code for IE7+, Firefox, Chrome, Opera, Safari
            xmlhttp = new XMLHttpRequest();
          } else {
            // code for IE6, IE5
            xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
          }
          xmlhttp.onreadystatechange = function() {
            if (this.readyState == 4 && this.status == 200) {
              document.getElementById(id).closest("tr").querySelector(".textHint").innerHTML = this.responseText;
            }
          };
          xmlhttp.open("GET", "getcategory.php?q=" + str, true);
          xmlhttp.send();
          */
      }
    }
    <form action="edit.php" method="post">
      <table cellpadding="0" cellspacing="0" border="0" class="table table-striped table-bordered">
        <tbody id="dataTable">
          <tr>
            <td>
              <select name="id" id="sel0" width="100%" onchange="showUser(this)">
                <option value="">SELECT A TOPIC</option>
                <option value="1">1</option>
                <option value="2">2</option>
                <option value="3">3</option>
              </select>
            </td>
            <td>
              <div class="txtHint"><b>Select a topic...</b></div>
            </td>
            <td>aaa</td>
          </tr>
        </tbody>
      </table>
      <br />
      <input type="button" class="btn btn-warning pull-left" value="Add another topic" onclick="addRow('dataTable')" />
      <button class="btn btn-success pull-right" name="submit_mult" type="submit" style="size:50;">
            Start Quiz
        </button>
    </form>

    </div>
    
    评论

报告相同问题?

悬赏问题

  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀