簡體   English   中英

分配給函數的變量(jQuery中的每個語句)沒有值

[英]Assigned variable to function(each statement in jQuery) not having values

我正在嘗試使用來自GitHub https://github.com/rbayliss/Dropdown-Table-Filter的 jQuery下拉過濾器插件。 在我的情況下,表格格式如下:

<table id="table_2">
    <thead>
    <tr>
      <th> Items </th>
      <th> Quantity </th>
    </tr>
    </thead>
    <tbody>
    <tr>
     <td> Apple </td>
     <td> 20 </td>
    </tr>
    <tr>
     <td> Orange </td>
     <td> 20 </td>
    </tr>
    </tbody>
</table>

因此,有必要對插件進行重新建模以適合這種情況:

var table = $(this);
    $('thead tr th:visible', table).each(function(index) {
         var selectbox = $('<select>');
         var values = [];
         var opts = new Array();
         selectbox.append('<option value="--all--">' + $(this).text() + '</option>');
     for(var noOfTr = 0; noOfTr<2; noOfTr++) {
          var tr = $('tbody tr:eq('+ noOfTr +')',table);
          var col = tr.find('td:eq(' + (index) + ')').each(function(index) {
            var cellVal = escape($.trim($(this).text()));
            if(cellVal.length == 0) {
            cellVal = '--empty--';
          }
          $(this).attr('ddtf-value', cellVal);

          if($.inArray(cellVal, values) === -1) {
            var cellText = $.trim($(this).text());
            if(cellText.length == 0) {cellText = '--Empty--';}
            values.push(cellVal);
            opts.push({val:cellVal, text:cellText});
          }
        });
    }       
}

執行后,分配給功能“ col”的變量不包含任何值,而“ opts”已正確填充。 請幫我解決問題。

嘗試使用:

var col = tr.find('td:eq(' + (noOfTr) + ')').each(function(index) {

代替:

var col = tr.find('td:eq(' + (index) + ')').each(function(index) {

因為自動增量變量是noOfTr不是index

暫無
暫無

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

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