使用 .each 分配多个元素属性?
所以我有下表,当我向表中添加一行时,我想将所有名称属性设置为新值。 (您可能猜到了:我使用按钮添加和删除行,并根据行索引计算新的名称属性)。
<table id="orderstable" >
<tbody>
<tr>
<td align="top"><img src="/images/images/add_button_sm.gif" id="add_" align="top" border="0">
<img src="/images/images/delete_button_sm.gif" id="del_" border="0">
</td>
<td><input name="orderBoxes[0].A" size="3" value="0.0" type="text"></td>
<td><input name="orderBoxes[0].B" size="3" value="0.0" type="text"></td>
<td><input name="orderBoxes[0].C" size="3" value="0.0" type="text"></td>
<td><select name="orderBoxes[0].D">
<option value="fifty">50</option>
<option value="oneHundred" selected="selected">100</option>
<option value="twoHundred">200</option>
<option value="threeHundred">300</option></select>
</td>
</tr>
</tbody>
</table>
我尝试使用 .each 获取所有元素,然后获取 input.name 属性......但我无法让它工作。有什么想法吗?我的代码可能不是最有效的......我仍然是一个菜鸟:
$("#orderstable > tbody > tr > td > img[id=add_]").click( function(event){
var row = $(this).closest("tr").get(0);
var rowCopy=$(row).clone(true);
$(row).closest("tbody").append(rowCopy);
$('#orderstable tbody>tr:last>td').each(function(){
$(this.input).attr(name);
}).val = "test";
row.className+="add_";
So I have the following table, and I'd like to set all the name attributes to a new value when I add a row to the table. (You probably guessed it: I use the buttons to add and delete rows, and I calculate a new name attribute based on the row index).
<table id="orderstable" >
<tbody>
<tr>
<td align="top"><img src="/images/images/add_button_sm.gif" id="add_" align="top" border="0">
<img src="/images/images/delete_button_sm.gif" id="del_" border="0">
</td>
<td><input name="orderBoxes[0].A" size="3" value="0.0" type="text"></td>
<td><input name="orderBoxes[0].B" size="3" value="0.0" type="text"></td>
<td><input name="orderBoxes[0].C" size="3" value="0.0" type="text"></td>
<td><select name="orderBoxes[0].D">
<option value="fifty">50</option>
<option value="oneHundred" selected="selected">100</option>
<option value="twoHundred">200</option>
<option value="threeHundred">300</option></select>
</td>
</tr>
</tbody>
</table>
I've tried to use .each to get all the elements and then get at the input.name attribbute.... but I can't get it to work. Any thoughts? My code may not be the most efficient... I'm still a noob:
$("#orderstable > tbody > tr > td > img[id=add_]").click( function(event){
var row = $(this).closest("tr").get(0);
var rowCopy=$(row).clone(true);
$(row).closest("tbody").append(rowCopy);
$('#orderstable tbody>tr:last>td').each(function(){
$(this.input).attr(name);
}).val = "test";
row.className+="add_";
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
![扫码二维码加入Web技术交流群](/public/img/jiaqun_03.jpg)
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这将抓取最后一行以及所有输入表单元素。然后你可以通过
$(this).attr('name')
设置name
This will grab the last row, and all input form elements. Then you can just set the
name
via$(this).attr('name')
在您的
.each
函数中,您需要使用参数来获取元素并找到输入:http://jsfiddle.net/swatkins/LFvwh/
In your
.each
function, you need to use the arguments to get at the element and find the input:http://jsfiddle.net/swatkins/LFvwh/
您的选择器似乎不正确。您的表没有 ID“orderstable”,也没有
。
Your selector does not seem to be correct. Your table doesn't have the id 'orderstable' and does not have a
<tbody>
.