jQuery .attr("disabled", "disabled") 在 Chrome 中不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6048022/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 20:18:03  来源:igfitidea点击:

jQuery .attr("disabled", "disabled") not working in Chrome

jquerygoogle-chromecross-browserattrjquery-attributes

提问by cabaret

Not sure why this isn't working.

不知道为什么这不起作用。

When people click the 'edit' button of my application, the disabled textfields become editable:

当人们单击我的应用程序的“编辑”按钮时,禁用的文本字段变得可编辑:

$("#bewerken").click(function(e)    {
    $("input[disabled='disabled']").removeAttr('disabled');
});

I then want to disable the textfields again when the user saves; I have this code bound to my save button:

然后我想在用户保存时再次禁用文本字段;我将此代码绑定到我的保存按钮:

$("#save_school_changes").click(function(e) {
    //stuff

    $.ajax({
        type: "POST",
        url: "/school/save_changes",
        data: { //stuff },
        success: function(data)
        {
            $("#feedback_top").html("<p>" + data['message'] + "</p>").slideDown('slow').delay(2000).slideUp();
            $("input[type='text']").attr('disabled', 'disabled');

        }
    });

    e.preventDefault();
});

As far as I know, this should disable the textfields again. However, this does not seem to be working in Chrome. It does work in Firefox. I haven't had the chance to test in IE or Safari yet. Is there any way to make this work in Chrome aswell? Thanks a lot!

据我所知,这应该再次禁用文本字段。但是,这似乎不适用于 Chrome。它确实在 Firefox 中工作。我还没有机会在 IE 或 Safari 中进行测试。有什么办法可以在 Chrome 中进行这项工作吗?非常感谢!

回答by Naftali aka Neal

If you are using jQuery < 1.6 do this:

如果您使用 jQuery < 1.6,请执行以下操作:

jQuery("input[type='text']").attr("disabled", 'disabled');

If you are using jQuery 1.6+:

如果您使用的是 jQuery 1.6+:

jQuery("input[type='text']").prop("disabled", true);

See this question: .prop() vs .attr()for references why.

请参阅此问题:.prop() vs .attr()以了解原因。

Or you can try this:

或者你可以试试这个:

$('input:text').attr("disabled", 'disabled');

see here for info on :text

有关信息,请参见此处 :text

回答by IQtheMC

For me, none of these answers worked, but I finally found one that did.

对我来说,这些答案都不起作用,但我终于找到了一个。

I needed this for IE-

我需要这个用于 IE-

$('input:text').attr("disabled", 'disabled');

I also had to add this for Chrome and Firefox -

我还必须为 Chrome 和 Firefox 添加这个 -

$('input:text').AddClass("notactive");

and this -

和这个 -

<style type="text/css">
    .notactive {
        pointer-events: none;
        cursor: default;
    }
 </style>

回答by RoyBS

It's an old post but I none of this solution worked for me so I'm posting my solution if anyone find this helpful.

这是一篇旧帖子,但我没有一个解决方案对我有用,所以如果有人觉得这有帮助,我会发布我的解决方案。

I just had the same problem.

我只是遇到了同样的问题。

In my case the control I needed to disable was a user control with child dropdowns which I could disable in IE but not in chrome.

在我的情况下,我需要禁用的控件是带有子下拉列表的用户控件,我可以在 IE 中禁用它,但不能在 chrome 中禁用。

my solution was to disable each child object, not just the usercontrol, with that code:

我的解决方案是使用该代码禁用每个子对象,而不仅仅是用户控件:

$('#controlName').find('*').each(function () { $(this).attr("disabled", true); })

It's working for me in chrome now.

它现在在 chrome 中为我工作。

回答by fanfavorite

if you are removing all disabled attributes from input, then why not just do:

如果您要从输入中删除所有禁用的属性,那么为什么不这样做:

$("input").removeAttr('disabled');

Then after ajax success:

然后在ajax成功后:

$("input[type='text']").attr('disabled', true);

Make sure you use remove the disabled attribute before submit, or it won't submit that data. If you need to submit it before changing, you need to use readonly instead.

确保在提交之前使用 remove disabled 属性,否则它不会提交该数据。如果需要在更改前提交,则需要改用readonly。

回答by racemic

My issue with this was that the element using the disabled attr needed to be defined as a form element, .ie input type for it to work. Both worked with attr() and prop() but chose the latter for future maintainability.

我的问题是使用禁用属性的元素需要定义为表单元素,即输入类型才能工作。两者都与 attr() 和 prop() 一起使用,但为了将来的可维护性选择了后者。

回答by Jyothise Johny

Mostly disabled attribute doesn't work with the anchor tags from HTML-5 onwards. Hence we have change it to ,let's say 'button' and style it accordingly with appropriate color,border-style etc. That's the most apt solution for any similar issue users are facing in Chrome . Only few elements support 'disabled' attribute: Span , select, option, textarea, input , button.

从 HTML-5 开始,大多数禁用属性不适用于锚标记。因此,我们将其更改为“按钮”,并使用适当的颜色、边框样式等对其进行相应的样式设置。对于用户在 Chrome 中面临的任何类似问题,这是最合适的解决方案。只有少数元素支持 'disabled' 属性:Span、select、option、textarea、input、button。

回答by RussellUresti

Try $("input[type='text']").attr('disabled', true);

尝试 $("input[type='text']").attr('disabled', true);

回答by Galled

Have you tried with prop()??

你试过prop()吗?

Well prop() seems works for me.

好吧 prop() 似乎对我有用

回答by Oscar Godson

Here:
http://jsbin.com/urize4/edit

在这里:http:
//jsbin.com/urize4/edit

Live Preview
http://jsbin.com/urize4/

实时预览
http://jsbin.com/urize4/

You should use "readonly" instead like:

你应该使用“ readonly”代替:

$("input[type='text']").attr("readonly", "true");