![](/img/trans.png)
[英]how to call a function with parameters that is assigned to a variable in JQuery .each()?
[英]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.