【问题标题】:How to fetch each td containits in a table, using Jquery如何使用 Jquery 获取表中的每个 td containsits
【发布时间】:2011-08-11 03:14:36
【问题描述】:

我正在动态生成一个表格。

我想获取我所有的 TD 包含,不包括 Thead 包含。

我的桌子看起来像这样:

<Html>
<table id="tblPhone">
  <thead>
    <tr>
      <th> Type </th>
      <th> Primary </th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td> Gen </td>
      <td> <input type="radio" CHECKED /> </td>
    </tr>
    <tr>
      <td> Mob </td>
      <td> <input type="radio" /> </td>
    </tr>
....
....
  </tbody>
</table>
</Html>

我需要获取数据并填充到下面的类 (C#):

class TelephoneType
{
  public string Type;
  public bool Primary;
}

我在按钮点击中使用了下面的 JS 函数,但它不起作用:(.

function RetriveTelephoneData() {

    var obj = $("#tblPhone");

    var hasTH = obj.find("thead").size() > 0;
    var hasTBody = obj.find("tbody").size() > 0;

    if (obj.is("table") && !hasTH) {
        var widths = [];

        // capture widths before rearranging
        obj.find("tr td").each(function(i) {
            widths.push($(this));
        });
    }
}

我是 Jquery 的新手。请帮我解决问题。 提前谢谢。

【问题讨论】:

  • 不知道为什么我的表格在我输入时没有呈现:(
  • 那不是 JavaScript。这看起来像 java。
  • 我能够修复它。使用代码按钮(由编辑器中的{} 按钮表示)
  • 您没有尝试,也没有错误。您只是要求我们为您做点什么,因为您不想尝试...继续设置jsfiddle。尽可能接近选择所有表格元素,然后我们可以提供帮助。
  • 我试过了,但是之前没有提到我的JS函数。现在我已经编辑了我的问题,也提到了这一点。

标签: jquery jquery-ui jquery-selectors


【解决方案1】:

试试这个作为你的按钮点击代码:

function Telephone(type,primary)
{
    this.Type=type;
    this.Primary=primary
}

function RetriveTelephoneData(){
    var phones=[];
    $("table#tblPhone > tbody > tr").each(function(){
    var tds=$(this).find("td");
    var t=new Telephone($(tds[0]).text(), $(tds[1]).find("input:radio").is(":checked"));
    phones.push(t);
}

首先创建一个 js 类来捕获值。 next 遍历tbody 中的所有行并找到type' andprimaryvalue of phone to create object ofTelephone` 类并将其推送到数组。现在你可以对数组的内容做任何你喜欢的事情了。

PS:- 您需要在单选按钮上添加 name 属性(与 name='primary' 相同的值。

【讨论】:

    【解决方案2】:

    不要在你的&lt;thead&gt; 中使用&lt;td&gt; 使用&lt;th&gt;

    <table>
      <thead>
        <tr>
           <th>Column1</th>
           <th>Column2</th>
        </tr>
      </thead>
      <tbody>
        <tr>
           <td> 0,1</td>
           <td> 1,1</td>
        </tr>
      </tbody>
    </table>
    

    等等……

    var allTd = $('td');
    

    【讨论】:

      【解决方案3】:

      试试这个

      function RetriveTelephoneData() {
      
          //var obj = $("#tblPhone");
      
          //var hasTH = obj.find("thead").size() > 0;
          //var hasTBody = obj.find("tbody").size() > 0;
      
          //if (obj.is("table") && !hasTH) {
              var widths = [];
      
              // capture widths before rearranging
              $("#tblPhone").find("td").each(function(i) {
                  widths.push($(this).width());
              });
          //}
      }
      

      【讨论】:

        猜你喜欢
        • 2015-08-16
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-10-03
        • 1970-01-01
        • 2010-09-14
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多