2014-01-15 133 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)...