$(this)选择器问题jquery

发布于 2024-10-23 23:46:13 字数 458 浏览 4 评论 0原文

我在这里有这个简单的 jquery 函数。单击一个按钮,我想在 ajax 之前提醒它自己的类,并在继承时再次提醒。但是最后一种情况下的选择器 "$(this)" 不起作用并且警报返回“未定义”..

为什么?

$(".button").live("click",function(){

alert($(this).attr('class')); //returns "button"
 $.ajax({
    type: "POST",
    url: myUrl,
    data: myData,
    cache: false,
    success: function(html)
    {
                alert($(this).attr('class')); //returns undefined

    }

});

i have this simple jquery function here.Clicking over a button i want to alert its own class before ajax and again upon succession..but the selector "$(this)" in the last situation is not working and the alert returns "undefined"..

why?

$(".button").live("click",function(){

alert($(this).attr('class')); //returns "button"
 $.ajax({
    type: "POST",
    url: myUrl,
    data: myData,
    cache: false,
    success: function(html)
    {
                alert($(this).attr('class')); //returns undefined

    }

});

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

爱本泡沫多脆弱 2024-10-30 23:46:13

我会这样做,将 $(this) 存储在一个变量中,这样您就可以在整个函数中使用它,而不必每次都执行 jQuery 查找,并且您也不必依赖于范围为 $(this) 提供正确的元素,

$(".button").live("click",function(){
    var button = $(this);
    alert(button.attr('class')); //returns "button"
    $.ajax({
        type: "POST",
        url: myUrl,
        data: myData,
        cache: false,
        success: function(html)
        {
            alert(button.attr('class')); //should also return "button"

        }
    });
});

仅包装 this 一次,这也是性能增强

I would do it like this, store $(this) in a variable so you can use it throughout the function without having to perform a jQuery lookup every time, and you also will not have to depend on the scope to provide the correct element for $(this)

$(".button").live("click",function(){
    var button = $(this);
    alert(button.attr('class')); //returns "button"
    $.ajax({
        type: "POST",
        url: myUrl,
        data: myData,
        cache: false,
        success: function(html)
        {
            alert(button.attr('class')); //should also return "button"

        }
    });
});

wrapping this only once also is a performance enhancement

橘虞初梦 2024-10-30 23:46:13

这将使它工作:

$(".button").live("click", function() {

    var button = this;

    $.ajax({
        type: "POST",
        url: myUrl,
        data: myData,
        cache: false,
        success: function(html) {
            alert($(button).attr('class')); 
        }
    });

});

您不能在嵌套函数内使用 this 引用。 success 函数是一个嵌套函数,它有自己的 this 值。如果您需要引用该嵌套函数内的按钮,则必须声明一个局部变量(如 button)。

function clickHandler() {

    // this == element that was clicked

    function ajaxHandler() {

        // this != element that was clicked 

    }

}

This will make it work:

$(".button").live("click", function() {

    var button = this;

    $.ajax({
        type: "POST",
        url: myUrl,
        data: myData,
        cache: false,
        success: function(html) {
            alert($(button).attr('class')); 
        }
    });

});

You cannot use the this reference inside nested functions. The success function is a nested function and it has its own this value. If you need the reference to the button inside that nested function, you have to declare a local variable (like button).

function clickHandler() {

    // this == element that was clicked

    function ajaxHandler() {

        // this != element that was clicked 

    }

}
淡写薰衣草的香 2024-10-30 23:46:13

尝试在声明函数时添加 var self = $(this); ,然后使用 self 而不是 $(this)

所以你的代码看起来像这样:

$(".button").live("click",function(){

var self = $(this);

alert($(this).attr('class')); //returns "button"
 $.ajax({
    type: "POST",
    url: myUrl,
    data: myData,
    cache: false,
    success: function(html)
    {
                alert(self.attr('class')); //returns undefined

    }
});

Try adding var self = $(this); when you declare the function, and then use self instead of $(this)

So your code looks like this:

$(".button").live("click",function(){

var self = $(this);

alert($(this).attr('class')); //returns "button"
 $.ajax({
    type: "POST",
    url: myUrl,
    data: myData,
    cache: false,
    success: function(html)
    {
                alert(self.attr('class')); //returns undefined

    }
});
尛丟丟 2024-10-30 23:46:13

很多人已经发布了解决方案,所以我不会发布代码。只是想提一下原因是因为由于 success 方法是回调,因此 $(this) 的上下文不再有效。所以你需要将它分配给一个变量并存储它以供你自己使用。

Lots of people have posted the solution for this so I won't post the code. Just wanted to mention the reason is because since the success method is a callback your context of $(this) isn't valid anymore. So you need to assign it to a variable and store it for your own use.

回忆追雨的时光 2024-10-30 23:46:13

$(this) 仅在引用 DOM 中的 HTML 对象时才存在。由于您已尝试在 AJAX 调用的成功函数中使用,因此 $(this) 没有引用。例如,在下面的代码中 $(this) 引用 jQuery 选择器返回的项目:

$('.button').each(function() {
    alert($(this));
});

您将需要使用选择器在全局范围内返回该项目,然后将其传递给AJAX 调用中的成功函数:

var myButton = $('.button');

$.ajax({
    type: "POST",
    url: myUrl,
    data: myData,
    cache: false,
    success: function(html) { alert(myButton.attr('class')); /* returns button */ }
});

$(this) only exists when referencing an HTML object in the DOM. Since you've tried using in the success function of the AJAX call, $(this) has no reference. So for example, in the following code $(this) refers to the item to returned by the jQuery selector:

$('.button').each(function() {
    alert($(this));
});

You will need to use a selector to return the item in global scope, and then pass this to the success function in the AJAX call:

var myButton = $('.button');

$.ajax({
    type: "POST",
    url: myUrl,
    data: myData,
    cache: false,
    success: function(html) { alert(myButton.attr('class')); /* returns button */ }
});
爱的故事 2024-10-30 23:46:13

请查看此处context部分。基本上,您的代码中似乎发生的情况是对 this 的引用不再适用。这是有道理的,因为在异步处理 AJAX 回调时代码的上下文已经发生变化。在 .ajax() 调用中将 context 显式设置为特定对象,将对上下文的引用携带到回调函数中。

Take a look at the context section here. Basically, what seems to be happening in your code is that the reference to this no longer applies. Makes sense, given that the context of the code has moved on while the AJAX callback is being handled asynchronously. Explicitly setting the context to a particular object in the .ajax() call will carry a reference to the context into the callback function.

刘备忘录 2024-10-30 23:46:13

您可以将 context: this 属性添加到传递给 $.ajax 调用的哈希中,这样 success 句柄将是正确设置上下文,或者您也可以执行类似的操作:

success: $.proxy(function(html) {  // using $.proxy will bind the function scope to this
    alert($(this).attr('class'));
}, this);

或者,我见过的另一种技术:

$(".button").live("click",function(){
    var self = this;
    alert($(self).attr('class')); //returns "button"
    $.ajax({
        type: "POST",
        url: myUrl,
        data: myData,
        cache: false,
        success: function(html)
        {
                alert($(self).attr('class')); //returns undefined

        }
});

You can either add a context: this property to the hash that is passed to the $.ajax call, that way the success handle will it's context set properly, or you can also do something like:

success: $.proxy(function(html) {  // using $.proxy will bind the function scope to this
    alert($(this).attr('class'));
}, this);

or, another technique I've seen:

$(".button").live("click",function(){
    var self = this;
    alert($(self).attr('class')); //returns "button"
    $.ajax({
        type: "POST",
        url: myUrl,
        data: myData,
        cache: false,
        success: function(html)
        {
                alert($(self).attr('class')); //returns undefined

        }
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文