从元素中获取类名称,然后选择具有相同类的所有元素

从元素中获取类名称,然后选择具有相同类的所有元素

问题描述:

$('.classToClick').live("click", function () {
    $(this).attr('disabled', 'disabled');
    $(this).val("lol");
    $.post('post.php', {
        string: "lalala"
    }, function (data) {
        $(this).removeAttr('disabled');
    });
})

此代码不起作用,因为在后期我们使用$(this).当我们将其更改为.classToClick时,它将起作用.

This code doesn't work, because in post we use $(this). When we change it to .classToClick, it works.

如何获得元素的类单击,以便我们可以像这样使用它?

How can we get the class of the element clicked so that we could use it like this?

$('.classToClick').live("click", function () {
    var className = $(this).name;
    $(this).attr('disabled', 'disabled');
    $(this).val("lol");
    $.post('post.php', {
        string: "lalala"
    }, function (data) {
        $(className).removeAttr('disabled');
    });
})

是否可以做类似的事情?

Is it possible to do something similar?

单击后,您需要捕获类名,可以通过以下方式完成:

Upon clicking, you will need to capture the class name, which can be done via:

var className = $(this).attr('class'); //Alternatively, $(this).prop('class')

,当您稍后在$选择器中引用它时,需要包括句点以表示正在选择一个类,例如:

and when you later refer to it inside of the $ selector, you need to include the period to denote a class is being selected, as such:

$('.' + className).removeAttr('disabled');

代码:

$('.classToClick').live("click", function() 
{   
    var className = $(this).attr('class');    
    $(this).attr('disabled', 'disabled').val("lol");

    $.post('post.php', {string : "lalala"}, function(data)
    {
        $('.' + className).removeAttr('disabled');
    });
}) 

工作示例

Working Example