2015-10-02 33 views
1

好吧,我有多個按鈕,它們使用不同的ID調用相同的javascript函數。jquery中的動態容器名稱

這裏是我的功能

function concur(id) { 
    //alert(id); 
    //throw ajax request 
    $.ajax({ //create an ajax request to load_page.php 
     type: "GET", 
     url: "ajax_getter.php?requestid=1&r=" + id, 
     dataType: "html", //expect html to be returned     
     success: function (response) { 
      $("#container").html(response); 
      //alert(response); 
     } 
    }); //ajax end 
} 

我會做什麼,如果我必須使id作爲容器名稱? 我的意思是,這個$("#id").html(response);是否適用於函數內所有動態變化的ID? 這不適合我。什麼是最好的解決方案?

回答

3

使用字符串連接在jQuery選擇器中使用變量。

$("#" + id).html(response); 
+3

@AlexChar是不是這個問題? – Jamiec