简体   繁体   English

如何验证动态html表中的输入?

[英]how to validate input in a dynamic html table?

I have the following code for generation of dynamic html which can accept input from the users, I want to validate the inputs of this table, I tried to add input ids in Javascript function, but it is not working, can you suggest any way to achieve this? 我有以下用于生成动态html的代码,该代码可以接受用户的输入,我想验证此表的输入,我尝试在Javascript函数中添加输入id,但是它不起作用,您能建议任何方法来实现这个?

HTML: HTML:

    <th>Number of BPIs: <title="Number of BPIs"></th>

  <td><select id="numberbpis" name="numberbpis" onchange="buildTable(this.value)">

  <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 >
  </select></td>
  </tr>



     <table id="contentTable" border="1">
      <!-- Fill table programmatically -->
     </table>

Javascript: Javascript:

function buildTable(val) {
    var myTable =document.getElementById("contentTable");
    var j=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>":"<input>";
            rows[rows.length - 1].appendChild(cells[i][x]);
        }
    }
}

buildTable();

I tried to add <input id="..." name="..."> , but this is not working, can you suggest any other solution ? 我试图添加<input id="..." name="..."> ,但这不起作用,您能建议其他解决方案吗?

this is the jsfiddle, i forgot to add some of the html code above, 这是jsfiddle,我忘了添加上面的一些html代码,

http://jsfiddle.net/FyAnR/2/ http://jsfiddle.net/FyAnR/2/

i want give an input id to the input in javascript function, so that i can use that id to call another javascript function, but when i give < input id="t1">, 我想给javascript函数中的输入一个输入id,以便我可以使用该id来调用另一个javascript函数,但是当我给<input id =“ t1”>时,

the dynamic table is not being generated, help ? 动态表没有生成,帮助吗?

i would validate a normal input in this way, 我将以这种方式验证正常输入,

          function AdaptiveValidate(){
       $adaptive = document.getElementById("adaptive").value;
       if(!/^-?\d*$/.test($adaptive)) {
           alert("Adaptive BPI vector(% capacity) value must be numeric!");
       } 
           } 

i want to use this to validate the elements of a table which are not yet created ? 我想用它来验证尚未创建的表元素?

and also how to read the input values from this table ? 以及如何从该表中读取输入值?

Can't comment but... 无法发表评论,但...

So you just want an id? 所以你只想要一个ID? So each one generated would show from t0 to tx? 因此,生成的每个数字都会显示从t0到tx? The way that I've done it dynamically adds an id per each cell. 我完成此操作的方式是为每个单元格动态添加一个ID。 Is this what you are looking for? 这是你想要的? http://jsfiddle.net/J4KFQ/ http://jsfiddle.net/J4KFQ/

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"+x+">":"<input id=t"+x+">";
        rows[rows.length - 1].appendChild(cells[i][x]);
}

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

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