如何从表格单元格的内容中提取值?

时间:2012-05-02 18:04:00

标签: jquery asp.net-mvc-3 html-table

我有一张包含4个cols的表:

<table id="myTable">
  <th>
    <td> A </td>
    <td> B </td>
    <td> C </td>
    <td> D </td>
  </th>
  <tr>
    <td> 
      <span>A-1</span>  <span>A-3</span>  <span>A-5</span>
    </td>
    <td> 
      <span>B-2</span> <span>B-3</span>
    </td>
    <td> 
      <span>C-3</span>   <span>C-4</span>   <span>C-7</span>
    </td>
    <td> 
      <span>D-1</span>
    </td>
  </tr>
</table>

我有A,B,C和D的下拉列表,带有更改处理程序(A = 1,B = 2等):

 @Html.DropDownListFor(model => model.TypeID, new SelectList(Model.Types, "Key", "Value", Model.TypeID))

  $('#myTable').change(function () {
    var type = $(this).val();
     if (type == 1) {
        var tr = $('#current tr')[0];
        var td = tr.cells[0].innerHTML;
        var last = $(td).find(':last-child');
        /// how do I get the contents of the last span???
     } else if ( type == 2 ) {
        ...
     }

  });

最后,一个文本框:

  @Html.TextBoxFor(model => model.next)

当用户从下拉列表中选择一个选项时,我想根据appropritate表格单元格中最后一个范围内的内容放置相应的“下一个”值。 我似乎无法弄清楚如何获取最后一个,并提取值。

1 个答案:

答案 0 :(得分:1)

$('#myTable').change(function () {
    var type = +$(this).val();
     if (type === 1) {
        var tr = $('#current tr');
        var td = tr.find('td:eq(0)');
        var last = td.find('span:last');
        var contents = last.html();
     } else if ( type == 2 ) {
        ...
     }

  });