获取表格中每个具有相同名称的input值

我有一个表,tbody是从node.js的数据响应dynamic填充的。 这就是为什么当我创buildinput名称和ID保持等于所有行。

表:

<table id="tblNotas"> <thead> <tr> <th>Column</th> <th>Column</th> <th>Input</th> </tr> </thead> <tbody> <% alunos.forEach(function(aluno) { %> <tr> <td><label>Some text</label></td> <td><label>Some text</label></td> <td> <input id="txtNota" name="txtNota" type="number" min="0.0" max="10" step="0.01"> </td> </tr> <% }); %> </tbody> </table> 

在JavaScript或JQuery中,我如何获得每行的inputtxtNota的值?

尝试这个:

 $('input[name="txtNota"]').each(function(key,val){ alert(val.value) }) 

尽pipe阅读select器。这可以扩展到类或任何HTML属性。 参考: https : //api.jquery.com/attribute-equals-selector/

假设你有类而不是ID(对于你的input),你可以这样做:

 function getValues() { var values = new Array(); $('#tblNotas tbody .txtNota').each(function (i,e) { values.push({ $(e).val() }) }); return values; } 

我不确定你在这里使用哪个模板引擎,但是你应该可以做这样的事情

 <% alunos.forEach(function(aluno) { %> <tr> <td><label>Some texto</label></td> <td><label>Some texto</label></td> <td> <input id="<%= aluno.id %>" name="txtNota" type="number" min="0.0" max="10" step="0.01"> </td> </tr> <% }); %> 

基本上,这个想法是从您正在迭代的对象中获取id,并将其传递到DOM元素的ID中,例如每个input。