簡體   English   中英

使用對象使用jQuery訪問表單元格

[英]Accessing table cells with jQuery using an object

我需要在多維數組中復制表格的單元格。 我嘗試以下代碼:

function tableManager ( table, rowitems ) {
    var items = array () ;

    var j = 0 ;
    var arow = array() ;
    $(table+' td').each(function() {
        arow[j] = $(this).html() ;
        j ++ ;
        if ( j == rowitems) {
            items.push(arow);
            j = 0 ;
        } ;
   }) ;

    this.show = function() {
    .............
    }
}

構造函數是:

$(document).ready(function(){
    var loadarr = new tableManager ( '#results', 4 ) ;
    ....
});

似乎不執行each()回調。

什么不對?

tableManager是一個函數,您不必使用“ new”構造它。 您必須按以下方式調用它:

$(document).ready(function(){
    var loadarr = tableManager('#results', 4);
    ...
});

另外,您還有一個“;” 在您的“ if(...){..}”之后。 您必須將其刪除:

if ( j == rowitems) {
        items.push(arow);
        j = 0 ;
    } ; <= REMOVE THIS

此處有更多詳細信息: https : //www.w3schools.com/js/js_function_definition.asp

安德烈

如果問題是:

似乎不執行each()回調。

這是因為tableManager是一個函數,您必須通過以下方式調用它:

var loadarr = tableManager ( '#results', 4 ) ;

有關更多信息,請訪問W3Schools。

另外,刪除; if ( j == rowitems)括號后

if ( j == rowitems) {
    items.push(arow);
    j = 0 ;
} // Removed ;

更新

如果未達到each方法,請嘗試了解正在發生的事情。 嘗試使用

console.log($(table+' td'));

在確保each方法都不會執行(因為沒有元素)之前。

console.log($(table+' td'));
$(table+' td').each(function() {
    arow[j] = $(this).html() ;
    ...

然后,您可以在參數函數中添加console.log("Each executed") execute console.log("Each executed")以確保它被執行:

$(table+' td').each(function() {
    console.log("Each executed");

    arow[j] = $(this).html() ;
    j ++ ;
    if ( j == rowitems) {
        items.push(arow);
        j = 0 ;
    }
}) ;

您的jquery函數不正確,請參見此處的工作代碼

    <!DOCTYPE html>
    <html>
    <head>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
    <style>
    table {
        font-family: arial, sans-serif;
        border-collapse: collapse;
        width: 100%;
    }

    td, th {
        border: 1px solid #dddddd;
        text-align: left;
        padding: 8px;
    }

    tr:nth-child(even) {
        background-color: #dddddd;
    }
    </style>
    </head>
    <body>

    <table id="results">
      <tr>
        <th>Company</th>
        <th>Contact</th>
        <th>Country</th>
      </tr>
      <tr>
        <td>Alfreds Futterkiste</td>
        <td>Maria Anders</td>
        <td>Germany</td>
      </tr>
      <tr>
        <td>Centro comercial Moctezuma</td>
        <td>Francisco Chang</td>
        <td>Mexico</td>
      </tr>
      <tr>
        <td>Ernst Handel</td>
        <td>Roland Mendel</td>
        <td>Austria</td>
      </tr>
      <tr>
        <td>Island Trading</td>
        <td>Helen Bennett</td>
        <td>UK</td>
      </tr>
      <tr>
        <td>Laughing Bacchus Winecellars</td>
        <td>Yoshi Tannamuri</td>
        <td>Canada</td>
      </tr>
      <tr>
        <td>Magazzini Alimentari Riuniti</td>
        <td>Giovanni Rovelli</td>
        <td>Italy</td>
      </tr>
    </table>

    </body>

    <script type="text/javascript">
      function tableManager ( table, rowitems ) {
                var items = [] ;

                $(table+' tr').each(function(i) {
                  var row = $(this);
                  if(rowitems == i){
                     row.find('td').each(function() {
                        console.log(this);
                        items.push($(this).html());
                     }) ; 
                  }
               });
                console.log(items);
               return items;
            }

      var itm = tableManager("#results", 4);
      console.log(itm);
    </script>
    </html>

我希望這能幫到您。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM