简体   繁体   English

分配给函数的变量(jQuery中的每个语句)没有值

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

I am trying to use this jQuery dropdown filter plugin from GitHub https://github.com/rbayliss/Dropdown-Table-Filter . 我正在尝试使用来自GitHub https://github.com/rbayliss/Dropdown-Table-Filter的 jQuery下拉过滤器插件。 In my case, the table format is like this: 在我的情况下,表格格式如下:

<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>

So, re-modelling the plugin to suit this case was necessary: 因此,有必要对插件进行重新建模以适合这种情况:

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});
          }
        });
    }       
}

The variable assigned to the function "col" is not containing any value after the execution whereas the "opts" is getting populated correctly. 执行后,分配给功能“ col”的变量不包含任何值,而“ opts”已正确填充。 Please help me out in populating col. 请帮我解决问题。

Try to use: 尝试使用:

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

instead of: 代替:

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

since the auto increment variable is noOfTr not index . 因为自动增量变量是noOfTr不是index

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM