[英]How to update HTML table cell using jquery
Team, 球队,
Could you please tell me how to update a cell using jquery? 您能否告诉我如何使用jquery更新单元格?
<table id='lop'>
<thead id='loph'>
<tr>
<td class='type'>Type</td>
<td class='name'>Name</td>
</tr>
</thead>
<tbody id='lopb'>
<tr id='1'>
<td class='type'>W</td>
<td class='name'>ZZZ</td>
<tr>
<tr id='2'>
<td class='type'>W</td>
<td class='name'>YYY</td>
<tr>
</tbody>
</table>
I would like to update second row name to XXX from YYY. 我想将第二行名称从YYY更新为XXX。
Please, The answer should be based on id and class name used in the html, not number/order based while using the jquery. 请,答案应基于html中使用的ID和类名,而不是基于使用jquery时的数字/顺序。
Also the below solution is not working, 另外下面的解决方案不起作用,
$('#lop #2 .name').html('XXX') //Working
in the case of 如果是
var rowID = '#2';
$('#lop rowID .name').html('XXX') //Not Working
Try this code, which is using the html method to update the code : 试试下面的代码,它使用html方法更新代码:
$('#lop #2 .name').html('XXX')
You can have a look to this fiddle : http://jsfiddle.net/cWQRY/ 您可以看一下这个小提琴: http : //jsfiddle.net/cWQRY/
If you want to do it with a variable, you can try this code : 如果要使用变量,可以尝试以下代码:
var rowID = '#2';
$('#lop '+rowID+' .name').html('XXX')
make sure you don't use integers as ids use string.. 确保您不使用整数,因为id使用字符串。
<tr id='id2'>
why: 为什么:
1) It violates W3C specification. 1)违反W3C规范。
2) It is detrimental to SEO. 2)这对SEO有害。
3) There can be server-side scripting issues 3)可能存在服务器端脚本问题
jquery jQuery的
$('#id2 .name').text('XXX')
尝试这个:
$('#2 .name).text('XXX');
$('#2 .name').html('XXX');
检查js小提琴: http : //jsfiddle.net/Xyn9e/7/
Try this:
* {
font-family:Consolas
}
.editableTable {
border:solid 1px;
width:100%
}
.editableTable td {
border:solid 1px;
}
.editableTable .cellEditing {
padding: 0;
}
.editableTable .cellEditing input[type=text]{
width:100%;
border:0;
background-color:rgb(255,253,210);
}
Example: http://mrbool.com/how-to-create-an-editable-html-table-with-jquery/27425
在Table中,在每个tr和td中使用ID并不是一个好习惯,最好使用index。
$('#lopb tr').filter(':eq(1) td.name').text('XXX');
$('#lop').each(function(){
var $row = $(this).parents('tr');
alert($row.find('td:eq(0)').html());
alert($row.find('td:eq(1)').html());
});
Please specify when you want to do this action of changing the cell data. 请指定您何时要执行更改单元格数据的操作。
For changing, please try, 如需更改,请尝试,
$('#2 .name').html('XXX')
Thanks Manikandan 谢谢Manikandan
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.