简体   繁体   English

如何从文本文件中预填充动态HTML表的值?

[英]how to pre-fill the values of a dynamic HTML table from a text file?

i have the following code in jsfiddle, http://jsfiddle.net/y3x4q/ 我在jsfiddle中有以下代码, http://jsfiddle.net/y3x4q/

JavaScript: JavaScript:

         function buildTable(val)
      {
   var myTable =document.getElementById("contentTable");
 var j=val;
var r1=val;
var rows = [];
var cells = [];

while (myTable.hasChildNodes()) {
myTable.removeChild(myTable.lastChild);
}


for( var i = 0; i < 1; i++ )
{
rows[i] = myTable.insertRow(i);
if(i%3==2)rows[i].addClass("every3rdrow");
cells[i] = [];

for( var x = 0; x < j ; x++ )
{
    cells[i][x] =document.createElement((x==0)?"th":"td");
    cells[i][x].innerHTML = (x==0)?"<input id=t onchange=ty() name=t[] size=3>":"<input id=t1 onchange=ty1() name=t1[] size=3>";
    rows[rows.length - 1].appendChild(cells[i][x]);
}
 }

  }
  buildTable();  

HTML: HTML:

  <tr>
   <th>Number of Models:<title="Number of Models"></th>

   <td><select id="numbermodels" name="numbermodels" onmouseover="buildTable(this.value)" onchange="buildTable(this.value);buildTable4(this.value);">

    <option value="1">1</option >
   <option selected="selected" value="2">2</option >
      < option value="3">3</option >
    <option value="4">4</option >
   <option value="5">5</option >
    <option value="6">6</option >
    </select></td>
     </tr><br>

     <tr>
     <th>Number of heads per model:</th>
      <td>
    <table id="contentTable" border="1" name="contentTable">
    <!-- Fill table programmatically -->
   </table></td>
      </tr>

how i can fill the values of the table on-load from an external text file ? 我如何从外部文本文件填充表中的值? i have tried to use php tags inside javascript, but i couldn't get it that way, can this be achieved by using getElementsByTagName ? 我试图在javascript中使用php标签,但我无法通过这种方式获取,可以通过使用getElementsByTagName来实现吗? how to do this ? 这个怎么做 ?

Your best bet is using AJAX if you are attempting to get content from an external file on load. 如果您尝试从加载的外部文件中获取内容,最好的选择是使用AJAX。 So use AJAX to get the data and probably JQuery to parse the data into your table. 因此,请使用AJAX获取数据,并可能使用JQuery将数据解析到表中。 You won't be able to use a CSS file. 您将无法使用CSS文件。 I would almost guess using an HTML file would be easiest. 我几乎猜到使用HTML文件是最简单的。 Is there a reason you are using an external text file instead of an array? 您使用外部文本文件而不是数组是有原因的吗?

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

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