Javascript 在jQuery函数调用中追加id?

Javascript 在jQuery函数调用中追加id?,javascript,jquery,Javascript,Jquery,是否有一种方法可以将handleResults()中的#dimensionName uU附加到add_filter_表单的当前_索引中 我有以下代码: this.add_filter_form = function (name, value, status) { form_count++; current_index++; form_status[current_index]

是否有一种方法可以将handleResults()中的#dimensionName uU附加到add_filter_表单的当前_索引中

我有以下代码:

this.add_filter_form = function (name, value, status) {
                    form_count++;
                    current_index++;
                    form_status[current_index] = status;

                    $("#_IWID_Filters_included").append("<div id='filter_" + current_index + "'>" +
                        "Filter name: <select id = 'dimensionName_" + current_index + "'></select>" +
                        "Filter value: <select id ='dimensionId_" + current_index + "'></select>" +
                        "<button type='button' id='remove_btn_" + current_index + "' onclick='filter_forms.remove_filter_form(" + current_index + ")' style='margin-top: 5px; margin-bottom: 5px;'>Remove filter</button>" +
                        "</div>");
                }

function handleResults(responseObj) 
            {
                //Populates every dimensionName_[current_index] with names:
                $("#dimensionName_").html(responseObj.DimensionListItem.map(function(item) 
                {
                    return $('<option>').text(item.dimensionDisplayName)[0];
                }));
            }

据我所知,您正在通过add_filter设置当前的_索引,并且需要在handleResults中异步使用该值。因此,您需要使此变量全局可用:

var current_index = 0;

this.add_filter_form = ...

function handleResults(responseObj){
  $("#dimensionName_" + current_index).html();
}

您需要将当前索引变量传递给handleResults函数。您还需要在当前_index变量的范围内调用该函数,或在其他函数的对象范围内保存副本。所以你可以使用这个.current\u索引

function handleResults(responseObj, current_index) {...
您需要连接字符串。用+号就可以了

$("#dimensionName_"+current_index)...

请你澄清一下你需要什么,或者发布一个带有虚拟值的简单示例;给定是我想要的,但代码显然不起作用:/因此,我们需要知道调用这些函数的顺序。请尝试
$(“#dimensionName_u”+current_index)
将特定的空css类添加到所有这些元素中,并通过该类而不是ID选择所有这些元素,剩下的就交给jQuery了?全局变量的问题总是初始化和重置!您可以在匿名函数闭包中执行此操作,完全没有问题
$("#dimensionName_"+current_index)...