jquery $(this) 而不是 $('#id 形式)

标签 jquery this

我正在这样做:

$("#bcdialog").dialog({
        resizable: true,
        height: 400,
        width: 600,
        modal: true,
        autoOpen: false,
        buttons: {
        'cancel': function() { $(this).dialog('close'); } 
        'save': function() { $("#bcdialog form").submit(); }
        }
    });

我想知道是否可以使用它来代替#bcdialog form

作为保存功能

最佳答案

也许给它上下文 this 会起作用,您仍然需要指定表单部分,但是给它上下文使它特定于您指定的选择器,而您没有对元素 ID 进行硬编码。

$("#bcdialog").dialog({
        resizable: true,
        height: 400,
        width: 600,
        modal: true,
        autoOpen: false,
        buttons: {
        'cancel': function() { $(this).dialog('close'); } 
        'save': function() { $("form",this).submit(); }
        }
    });

:-)

关于jquery $(this) 而不是 $('#id 形式),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3101166/

相关文章:

javascript - 与此等效的 jQuery 是什么,它有什么作用?

javascript - Typescript 类中的 jQuery 回调

java - 为什么 "this"用作 RelativeLayout 构造函数中的参数?

Javascript:使用 'undefined' 访问 'this' 子函数中的外部范围变量时得到 'private'

c++ - 使用 new(this) 重用构造函数

jquery - 如何在标题旁边(而不是标题下方)放置一个显示/隐藏切换按钮?

php - 让ajax不断发送数据到php脚本并检索结果?

javascript - 如何使用 javascript 和 html 中的范围 slider 更改图像大小?

jquery - 浏览到目标常见问题解答主题

C# 在继承 get 中使用 base 或 this