0

我将“禁用”属性textbox动态添加到输入字段中jQuery。但是查看生成的,它在我添加了 'disabled' 属性HTML的输入字段上不可见。textbox

而且因为它没有被添加到其中,所以我的其他 jQuery 函数永远不会被输入。

这是我的代码:

这发生在文档加载事件中的某个地方:

$('.testGroup :input').prop('disabled', true)

click当您或hover禁用输入时,此功能会显示工具提示:

$('input:disabled').after(function (e) {
        debugger;
        d = $("<div>");
        i = $(this);
        d.css({
            height: i.outerHeight(),
            width: i.outerWidth(),
            position: "absolute",
        })
        d.css(i.offset());
        d.attr("title", i.attr("title"));
        d.tooltip();
        return d;
    });

我已经使用/尝试过prop()and attr(),但没有一个进入我的after()函数。

根据Paul Rosania的说法,我应该使用prop().

还有另一种方法可以做到这一点吗?

编辑jsFiddle

4

1 回答 1

1

问题是添加工具提示的代码仅在 DOM 准备好后运行一次。您需要为您禁用的每个元素调用它。

一个简单的解决方案是为您的工具提示编写一个方法:

function AddToolTip() {
    d = $("<div>");
    i = $(this);
    d.css({
        height: i.outerHeight(),
        width: i.outerWidth(),
        position: "absolute",
    })
    d.css(i.offset());
    d.attr("title", i.attr("title"));
    d.tooltip();
    return d;
}

然后,使用此方法将工具提示应用于在 DOM 就绪时禁用的元素

$('input:disabled, button:disabled').after(AddToolTip);

在禁用输入元素时,再次调用该方法

$('#test').prop('disabled', true).after(AddToolTip);

小提琴

于 2014-01-19T14:28:50.067 回答