简体   繁体   English

如何访问HTML表格单元格中的选择

[英]How can I access a select in an HTML table cell

I added a select to a column of table cells with an ID. 我将选择添加到具有ID的表单元格的列中。 Using console.log I can see the select with the ID I gave it but when I try to set the value of the box using the ID I get a NULL reference error. 使用console.log,我可以看到带有给定ID的select,但是当我尝试使用ID设置框的值时,出现NULL引用错误。 What would the correct reference be for the box? 盒子的正确参考是什么? Thanks. 谢谢。

JavaScript JavaScript的

function GetProc_Responce(r, responce) {
  var table = "<tr><th>Emp</th><th>RA</th><th>PI</th><th>First Name</th><th>Last Name</th><th>Is A</th><th>Date Created</th><th>Date Modified</th></tr>";
  strXml = new XMLSerializer().serializeToString(responce);

  //console.log("returnString: " + strXml);
  var oParser = new DOMParser();
  oDOM = oParser.parseFromString(strXml, "text/xml");

  //console.log(oDOM.getElementsByTagName("EmployeeID").length);
  var l = oDOM.getElementsByTagName("EmployeeID").length;
  for (i = 0; i <= l - 1; i++) {
    a = oDOM.getElementsByTagName("Emp")[i];
    _Emp = a.childNodes[0].nodeValue;
    b = oDOM.getElementsByTagName("RA")[i];
    _RA = b.childNodes[0].nodeValue;
    c = oDOM.getElementsByTagName("PI")[i];
    _PI = c.childNodes[0].nodeValue;
    d = oDOM.getElementsByTagName("FirstName")[i];
    _FirstName = d.childNodes[0].nodeValue;
    e = oDOM.getElementsByTagName("LastName")[i];
    _LastName = e.childNodes[0].nodeValue;
    f = oDOM.getElementsByTagName("IsA")[i];
    _IsA = f.childNodes[0].nodeValue;
    g = oDOM.getElementsByTagName("DateCreated")[i];
    _DateCreated = g.childNodes[0].nodeValue;
    h = oDOM.getElementsByTagName("DateModified")[i];
    _DateModified = h.childNodes[0].nodeValue;
    table += "<tr><td>" +
      a.childNodes[0].nodeValue + "</td><td>" +
      b.childNodes[0].nodeValue + "</td><td>" +
      c.childNodes[0].nodeValue + "</td><td>" +
      d.childNodes[0].nodeValue + "</td><td>" +
      e.childNodes[0].nodeValue + "</td><td>" +
      //f.childNodes[0].nodeValue + "</td><td>" +
      "<select id=\"s1\"><option value=\"0\">0</option><option value=\"1\">1</option></select>" + "</td><td>" +
      g.childNodes[0].nodeValue + "</td><td>" +
      h.childNodes[0].nodeValue + "</td></tr>";

    document.getElementById('Proc').rows.item(3).cells(5).value = 1;
    //OR
    document.getElementById('s1').selectedValue = 1;
    //NEITHER ONE WORKS


  }
  document.getElementById("Proc").innerHTML = table;

  console.log(document.getElementById('Proc').rows(3).cells(5));
}

HTML HTML

<div><center>
  <table id="Proc"></table>
</center></div>

You must assign unique id values. 您必须分配唯一的ID值。 Your code assigns all of them the id value s1 which is invalid in HTML. 您的代码为所有代码分配了IDs1 (在HTML中无效)。

Change your code as follows to assign s0 , s1 , s2 ... etc. For clarity I don't repeat the code that is not concerned: 如下更改代码以分配s0s1s2 ...等。为清楚起见,我不再重复不涉及的代码:

for (i = 0; i <= l - 1; i++)
{
    // ...
    table += "<tr><td>" +
            // ...
            e.childNodes[0].nodeValue + "</td><td>" +
            "<select id=\"s" + i + "\"><option value=\"0\">0</option><option value=\"1\">1</option></select>" + "</td><td>" +
            // ...
 }

You can access one of the select elements by its id with getElementById : 您可以使用getElementById通过其ID访问select元素之一:

var element = document.getElementById('s' + i);

Where i is a number from 0 to the last one assigned in the loop. 其中, i是从0到循环中分配的最后一个数字。

You can get or set the value of a select element via its value attribute. 您可以通过select元素的value属性获取或设置其value For example, to set its value to 1, do: 例如,要将其值设置为1,请执行以下操作:

document.getElementById('s' + i).value = '1';

document.getElementById('Proc').rows.item(3).cells(5) references a td, which does not have a value. document.getElementById('Proc').rows.item(3).cells(5)引用一个td,它没有值。

document.getElementById('s1') references the select box document.getElementById('s1')引用选择框

you want document.getElementById('Proc').rows.item(3).cells(5).children[0] to reference the select 您希望document.getElementById('Proc').rows.item(3).cells(5).children[0]引用选择

A better method would be to use some of the built in tag finding functions such as document.getElementById('Proc').rows[3].getElementsByTagName('select')[0] . 更好的方法是使用一些内置的标记查找功能,例如document.getElementById('Proc').rows[3].getElementsByTagName('select')[0] Then you don't have to deal with what column the select is in. Of course, if you have more than one select on the row, then you'll need to do something different. 然后,您不必处理选择所在的列。当然,如果该行上有多个选择,则需要做一些不同的事情。 At that point, I'd suggest adding class names to your selects so you can use document.getElementById('Proc').rows[3].getElementsByClassName('select1')[0] 那时,我建议将类名添加到您的选择中,以便可以使用document.getElementById('Proc').rows[3].getElementsByClassName('select1')[0]

Here we'll get div object 在这里,我们将获得div对象

var div     = document.getElementById('test');

Here we'll get table object 在这里,我们将获得表对象

var table = div.getElementsByTagName('table')[0];

Here we'll add new select to td 在这里,我们将新选择添加到td

table.rows[0].cells[0].innerHTML += '<select id="s2"></select>';

Search existed select (with id="s1") 搜索存在选择(ID为=“ s1”)

var select_1 = table.rows[0].cells[0].getElementsByTagName('select')[0];
console.log(select_1);

Search all selects in td 搜索TD中的所有选择

var select_array = table.rows[0].cells[0].getElementsByTagName('select');
console.log(select_array);

https://jsfiddle.net/e59dzhsy/ https://jsfiddle.net/e59dzhsy/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM