获取表格中每个具有相同名称的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。
- 错误:ENOENT:没有这样的文件或目录,打开“./views/s.ejs”Nodejs Express
- 将正则expression式放在string中
- 给定一个URL,脚本怎么能find哪些资源被加载?
- $ location.path不使用Angularjsbutton点击加载html文件
- Node.js:提交时如何阅读用户select选项并在textarea中提供文件内容
- Socket.IO接受不存在的src标签?
- Node.js Cheerioparsinghtml表格内的html表格
- AngularJS从另一个数据模型访问数据模型
- 无法连接到来自通过不同的服务器或文件系统提供的HTML文件的socket.io