用它来改变嵌套函数中的值($ .get)

时间:2012-03-23 15:57:20

标签: jquery

我正在尝试使用jQuery来更改button.kill的值。但是我不知道如何从get回调中访问button.kill。

$("button.kill").click(function(){
    $.get("file.php",{ nbRandom: Math.random() },
    function(data){
        //set button.kill text to data
    });
});

将有多个名为kill的按钮,我想确保我只更新用户点击过的按钮。

任何指针?

4 个答案:

答案 0 :(得分:1)

你可以做到

$("button.kill").click(function(){
    //save the variable that refers to the current object  to another variable
    var that = this;
    $.get("file.php",{ nbRandom: Math.random() },
    function(data){
        //set button.kill text to data
        //here this would not be the clicked element so we use that
         $(that).text(data);
    });
});

答案 1 :(得分:0)

以下是:

$("button.kill").click(function(){
    var t = $(this);
    $.get("file.php",{ nbRandom: Math.random() },
    function(data){
        t.text(data);
    });
});

答案 2 :(得分:0)

这应该有效:

$("button.kill").click(function(){
    var that = $(this);

    $.get("file.php",{ nbRandom: Math.random() },
    function(data){
        var text = that.val();
        //set button.kill text to data
    });

});

答案 3 :(得分:0)

$("button.kill").click(function(){
    var item=$(this);
    $.get("file.php",{ nbRandom: Math.random() },
    function(data){
        item.text(data);
    });
});