简体   繁体   中英

How to update HTML table cell using jquery

Team,

Could you please tell me how to update a cell using 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.
Please, The answer should be based on id and class name used in the html, not number/order based while using the 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 :

$('#lop #2 .name').html('XXX')

You can have a look to this fiddle : 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..

 <tr id='id2'>

why:

1) It violates W3C specification.

2) It is detrimental to SEO.

3) There can be server-side scripting issues

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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