如何使这个 jquery 函数更简洁?

发布于 2024-10-15 23:53:41 字数 1440 浏览 3 评论 0原文

我的 asp.net 项目中的一个通用 javascript 文件中有此代码。

每当我将鼠标悬停在受此函数影响的按钮之一上时,jQuery-Lint 就会返回“您多次使用同一个选择器”。

//turns all the buttons into jqueryUI buttons
//#mainBody is on the master page, #childBody is on the modal page.
$("#mainBody button, #mainBody input:submit, #mainBody input:button, #childBody button, #childBody input:submit, #childBody input:button").livequery(function () {
    $(this).button().each(function (index) {
                            $(this).ajaxStart(function () {
                                    $.data(this, "old_button_val", $(this).val());
                                    $.data(this, "old_button_disabled", $(this).button("option", "disabled"));
                                    $(this).button("option", "disabled", true).val("Wait...");
                                }).ajaxStop(function () {
                                    $(this).val($.data(this, "old_button_val")).button("option", "disabled", $.data(this, "old_button_disabled"));
                                }).ajaxError(function () {
                                    $(this).val($.data(this, "old_button_val")).button("option", "disabled", $.data(this, "old_button_disabled"));
                                });
                        });
});

有人提出了类似的问题这里

I have this code in a common javascript file in my asp.net project.

jQuery-Lint returns "You've used the same selector more than once" whenever I mouse over one of the buttons that was affected by this function.

//turns all the buttons into jqueryUI buttons
//#mainBody is on the master page, #childBody is on the modal page.
$("#mainBody button, #mainBody input:submit, #mainBody input:button, #childBody button, #childBody input:submit, #childBody input:button").livequery(function () {
    $(this).button().each(function (index) {
                            $(this).ajaxStart(function () {
                                    $.data(this, "old_button_val", $(this).val());
                                    $.data(this, "old_button_disabled", $(this).button("option", "disabled"));
                                    $(this).button("option", "disabled", true).val("Wait...");
                                }).ajaxStop(function () {
                                    $(this).val($.data(this, "old_button_val")).button("option", "disabled", $.data(this, "old_button_disabled"));
                                }).ajaxError(function () {
                                    $(this).val($.data(this, "old_button_val")).button("option", "disabled", $.data(this, "old_button_disabled"));
                                });
                        });
});

A similar question was asked here.

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

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

发布评论

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

评论(1

Smile简单爱 2024-10-22 23:53:41
// Might be a good idea now to add a class to these element
// instead of using a long selector like this
// Additionally, :button already includes <button> elements
var selector = "#mainBody input:submit, #mainBody input:button, #childBody input:submit, #childBody input:button";

$(selector).livequery(function() {
    // Store a copy of $(this), which we'll reuse... and reuse... and reuse
    var t = $(this);

    // Create the callback function shared berween
    // ajaxStop and ajaxError
    function ajaxCallback () {
        t.button('option', {
             label: t.data("old_button_val"),
             disabled: t.data('old_button_disabled')
         });
    }

    t.button()
        .ajaxStart(function() {
            // Use $.fn.data instead of $.data
            t.data({
                // Using 'label' instead of 'val'
                // because <button> elements do not have 'value's
                "old_button_val", t.button('option', 'label'),
                "old_button_disabled": t.button("option", "disabled")
            }).button('option', {
                disabled: true,
                label: 'Wait...'
            });
        }).ajaxStop(ajaxCallback).ajaxError(ajaxCallback);
    });
});

免责声明:未经测试,因此不保证有效。

// Might be a good idea now to add a class to these element
// instead of using a long selector like this
// Additionally, :button already includes <button> elements
var selector = "#mainBody input:submit, #mainBody input:button, #childBody input:submit, #childBody input:button";

$(selector).livequery(function() {
    // Store a copy of $(this), which we'll reuse... and reuse... and reuse
    var t = $(this);

    // Create the callback function shared berween
    // ajaxStop and ajaxError
    function ajaxCallback () {
        t.button('option', {
             label: t.data("old_button_val"),
             disabled: t.data('old_button_disabled')
         });
    }

    t.button()
        .ajaxStart(function() {
            // Use $.fn.data instead of $.data
            t.data({
                // Using 'label' instead of 'val'
                // because <button> elements do not have 'value's
                "old_button_val", t.button('option', 'label'),
                "old_button_disabled": t.button("option", "disabled")
            }).button('option', {
                disabled: true,
                label: 'Wait...'
            });
        }).ajaxStop(ajaxCallback).ajaxError(ajaxCallback);
    });
});

Disclaimer: Not tested, therefore not guaranteed to work.

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