2014-01-15 89 views
1

是否有一种方法来在handle_result()中添加#dimensionName_和current_index的add_filter_Form?在jQuery函数调用中追加id?

我有以下代码:

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

进出口寻找沿的效果的东西:

$("#'dimensionName_ + current_index'") 
+0

您能否澄清一下您需要的内容或发布带有虚拟值的简单示例。 – user1853181

+0

我更新了问题;给出的是我想要的,但该代码显然不工作:/ – Xenyal

+0

因此,我们应该需要知道这些函数被称为 – Alex

回答

1

据我了解,你是通过设置在的add_filter和CURRENT_INDEX需要这个值异步在handleResults内部。因此,您需要使此变量全局可用:

var current_index = 0; 

this.add_filter_form = ... 

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

全局变量的问题始终是初始化和重置! –

+0

你可以在匿名函数关闭中做到这一点,完全没有问题 – Alex

1

您需要将当前索引变量传递给handleResults函数。您还需要在current_index变量的作用域中调用该函数,或者从另一个函数中将对象作用域保存在对象作用域中。所以你可以使用this.current_index

function handleResults(responseObj, current_index) {... 

你需要连接字符串。只需使用+符号即可。

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