获取jQuery筛选器以接受整数变量
我正在尝试使用jQuery集合的获取jQuery筛选器以接受整数变量,jquery,variables,filter,Jquery,Variables,Filter,我正在尝试使用jQuery集合的gt()过滤器。我希望清除索引高于触发更改事件的元素的所有选定元素。我确信这与无法获取gt()以接受变量thisIndex作为整数有关,而且似乎找不到这样做的语法 $(document).ready(function () { $(".slct").change(function() { // sensitize all slct elements thisIndex = $(".slct").index(this);
gt()
过滤器。我希望清除索引高于触发更改事件的元素的所有选定元素。我确信这与无法获取gt()
以接受变量thisIndex
作为整数有关,而且似乎找不到这样做的语法
$(document).ready(function () {
$(".slct").change(function() { // sensitize all slct elements
thisIndex = $(".slct").index(this);
selGT1 = $(".slct:gt(1)"); // TEST: this correctly selects the elements greater than 1
selectsGT = $(".slct:gt(thisIndex)"); // "I WANT thisIndex TO ACT LIKE A INTEGER"
$(selectsGT).val(""); // clear elements w/ a index greater than "this"
});
});
当前在代码中,“thisIndex”被解释为字符串的一部分。您需要将整数与字符串连接起来。所以下面的代码片段应该可以做到这一点:
selectsGT = $(".slct:gt(" + thisIndex + ")");
jQuery选择器是一个字符串。您必须使用字符串加法来构建正确的字符串,以构造一个将数值作为字符串包含在其中的字符串。