根据单元格值jquery更新表行

时间:2016-03-01 02:15:44

标签: jquery

我有一个联系人表格,其中包含contact_id的列。有人可以帮我更新基于contact_id的表格行吗?例如,我有下表:

<table class="table table-condensed" id="company_contacts_table">
    <thead>
      <tr>
        <th>First Name</th>
        <th>Last Name</th>
        <th>Email</th>
        <th>Phone #</th>
        <th>Notes</th>
        <th></th>
        <th></th>
      <th hidden></th>
      </tr>
    </thead>
    <tbody>
      <tr>
        <td>Jake</td>
        <td>Dang</td>
        <td>jake123@gmail.com</td>
        <td>7032669955</td>
        <td>contact_notes</td>
        <td><button type="update" class="btn btn-primary update_contact" href="#update_contact_modal" data-toggle="modal" id="some_contact_id">Update</button></td>
        <td><button type="update" class="btn btn-danger delete_contact" id="some_contact_id">Delete</button></td>
        <td hidden>some_contact_id</td>
      </tr>
      <tr>
        <td>Harrison</td>
        <td>WElls</td>
        <td>Hwells@gmail.com</td>
        <td>7039998888</td>
        <td>contact_notes</td>
        <td><button type="update" class="btn btn-primary update_contact" href="#update_contact_modal" data-toggle="modal" id="some_contact_id2">Update</button></td>
        <td><button type="update" class="btn btn-danger delete_contact" id="some_contact_id2">Delete</button></td>
        <td hidden>some_contact_id2</td>
      </tr>
      <tr>
        <td>Mason</td>
        <td>Hanley</td>
        <td>mnahley@gmail.com</td>
        <td>7032669855</td>
        <td>contact_notes</td>
        <td><button type="update" class="btn btn-primary update_contact" href="#update_contact_modal" data-toggle="modal" id="some_contact_id3">Update</button></td>
        <td><button type="update" class="btn btn-danger delete_contact" id="some_contact_id3">Delete</button></td>
        <td hidden>some_contact_id3</td>
      </tr>
    </tbody>
</table>

每行包含一个隐藏的contact_id列。我想根据联系人ID更新名字,姓氏,电子邮件,电话号码,备注。样本伪代码:

var my_contact_id = some_contact_id

var new_first_name = 'george'
var new_last_name = 'henry'
var new_email = 'tinpopo@hotmail.com'
var new_phone = 999-333-4444

- Find row that contains `some_contact_id`
- Set first name, last name, email, phone to new first name, new last name, new email, new phone

有人可以帮忙吗?

提前致谢!!

1 个答案:

答案 0 :(得分:2)

var new_first_name = 'george'
var new_last_name = 'henry'
var new_email = 'tinpopo@hotmail.com'
var new_phone = 999-333-4444

var tr = $("#company_contacts_table td:contains("+some_contact_id+")").closest("tr").find("td");
tr[0].innerHTML = new_first_name;
tr[1].innerHTML = new_last_name;
tr[2].innerHTML = new_email;
tr[3].innerHTML = new_phone;