jquery - 在 JQuery 中引用类的当前对象

标签 jquery jquery-selectors this

对于任何类型的 Web 开发,我都不是很有经验,更不用说 jquery 和 ajax,我需要使用它们,所以我遇到了困难。

我正在开发一个项目,该项目要求我在单击名为“重新打开”的单独按钮时将两个复选框重新绘制为“未选中”。现在我正在使用的代码是:

$('.reopenBtn').click(function() {
    var confirm_reopen = confirm("Are you sure you want to open this opportunity back up?");
    if (confirm_reopen) {
        var href = 'cfcs/opportunity.cfc';
        var method = 'ReopenOp';
        var opsid = $(this).parent().parent().attr('id');
        $('#' + opsid).ajaxSuccess(function() {});
        $.post(href, {
            method: method,
            opsid: opsid
        });
        $('.lost').removeAttr('checked');
        $('.won').removeAttr('checked');
    }
});

此代码的问题在于,它重新绘制了页面上复选框的所有实例,而不仅仅是赢得和失去的复选框的特定实例。我正在寻找的是这样的:

$(this).('.lost').removeAttr('checked');
$(this).('.won').removeAttr('checcked');

就像我说的,我对大多数 Web 开发语言都不是很有经验,所以我真的不知道能够做到这一点的语法。我的限制只是能够阅读和理解代码足以调整和更改它,而不是从头开始任何新的东西。

任何帮助将不胜感激!

最佳答案

您可以将其作为选择器中的上下文传递 http://api.jquery.com/jQuery/#selector-context

$('.lost,.won',this).removeAttr('checked');

仅供引用,.prop()从 jQuery 1.6+ 开始,这是在属性中设置禁用的正确方法

关于jquery - 在 JQuery 中引用类的当前对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13461908/

相关文章:

javascript - 完全加载后将事件分配给 div

jquery - 我怎样才能让我的 jQuery 'Click' 函数只为直接 li 运行,而不是子 li 的

jQuery 查找第一行

javascript - 是否可以创建导航祖先的自定义 jQuery 选择器?例如:closest or :parents selector

PHP: `array(&$this)` 是什么意思?

C# 代码样式 : Switching from "this." prefix to "underscore"

javascript - 如何在播放 HTML 5 视频(如 Youtube 广告)时添加图像/DIV?

jquery - Ajax 调用中无法识别 AntiforgeryToken

jquery - Ajax请求后如何重新生成jQuery Mobile样式?

c# - .NET 文档中 'this' 关键字的定义