开发者

Can't use dynamic variable into JQuery function

开发者 https://www.devze.com 2023-03-22 14:51 出处:网络
I want to disable TableSorter plugin on some th. These th have class=\"disableSorter\", so I do: $(\'.disableSorter\').each(function(index) {

I want to disable TableSorter plugin on some th. These th have class="disableSorter", so I do:

$('.disableSorter').each(function(index) {

    $('.tablesorter').tablesorter({ headers: { index: { sorter: false} } }); 

});

where index: is the index variable passed in the function. The thing is " index " is 开发者_开发百科not replaced by the number (for example 0:, 1: 5:, etc.)

Thanks for help!

===== Edit =====

Thanks JaredPar, your solution was right about the dynamic variable. I had to edit the code because I wan't getting the index of the th element, but the index of the each loop.

Here's my final code

var inner = {};
$('.disableSorter').each(function() {
    inner[$('.tablesorter th').index(this)] = { sorter: false };
});

$('.tablesorter').tablesorter({ headers: inner  }); 


The problem here is you're trying to use the value in a place where javascript is not looking for values but instead for literals to use as names. You need to use the [] syntax to create a named member based on a value.

Try the following

$('.disableSorter').each(function(index) {
  var inner = {};
  inner[index] = { sorter: false };
  $('.tablesorter').tablesorter({ headers: inner); 

});


Try this

var obj = null;
$('.disableSorter').each(function(index) {
    obj = {};
    obj.headers = {};
    obj.headers[index] = { sorter: false };
    $('.tablesorter').tablesorter(obj); 

});
0

精彩评论

暂无评论...
验证码 换一张
取 消