如何将链式函数分配给 jQuery 中的变量

How to assign chained functions to a variable in jQuery

例如,我想将一个链式流程分配给不同的变量,并在不同的条件下使用它们。

var disable_btn = true;
var button_me = $('#contents.getsamplekit .sample-form .form #submit-btn-freeSample-me');
var button_disabled = button_me.attr("disabled",true).css("background","red"); // need to add more functions.
var button_enabled = button_me.removeAttr("disabled").css("background","green"); // need to add more functions here as well.
(disable_btn) ? button_disabled : button_enabled; 
]

您是在分配变量时调用函数,而不是在测试时调用函数 disable_btn。如果你想等待,你必须定义一个新的函数:

function button_disable(button) {
    button.attr("disabled", true).css("background", "red");
}
function button_enable(button) {
    button.removeAttr("disabled").css("background","green");
}
(disable_btn ? button_enable : button_disable)(button_me);