jQuery .attr(“禁用”、“禁用”)不能在Chrome中工作

时间:2021-12-14 17:04:35

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按钮:

$("#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!

就我所知,这应该再次禁用textfields。然而,这在Chrome中似乎并不适用。它在Firefox中确实有用。我还没有机会在IE或Safari中测试。有什么方法可以在Chrome上也这样做吗?谢谢!

8 个解决方案

#1


69  

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()与.attr()以了解为什么。

Or you can try this:

或者你可以试试这个:

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

see here for info on :text

有关:文本的信息请参见这里

#2


3  

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>

#3


2  

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:

我的解决方案是使用这些代码禁用每个子对象,而不仅仅是usercontrol:

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

It's working for me in chrome now.

它现在在chrome上对我有用。

#4


0  

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

试着$("输入type = '文本']”)。attr(“禁用”,真正的);

#5


0  

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");

#6


0  

Have you tried with prop() ??

你试过“道具”吗?

Well prop() seems works for me.

我觉得prop()应该没问题。

#7


0  

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.

确保您在提交之前使用了移除禁用属性,否则它不会提交数据。如果您需要在更改之前提交它,则需要使用readonly。

#8


0  

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的元素需要定义为一个表单元素。ie输入类型,以便其工作。两者都使用了attr()和prop(),但是为了将来的可维护性,选择了后者。

#1


69  

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()与.attr()以了解为什么。

Or you can try this:

或者你可以试试这个:

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

see here for info on :text

有关:文本的信息请参见这里

#2


3  

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>

#3


2  

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:

我的解决方案是使用这些代码禁用每个子对象,而不仅仅是usercontrol:

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

It's working for me in chrome now.

它现在在chrome上对我有用。

#4


0  

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

试着$("输入type = '文本']”)。attr(“禁用”,真正的);

#5


0  

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");

#6


0  

Have you tried with prop() ??

你试过“道具”吗?

Well prop() seems works for me.

我觉得prop()应该没问题。

#7


0  

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.

确保您在提交之前使用了移除禁用属性,否则它不会提交数据。如果您需要在更改之前提交它,则需要使用readonly。

#8


0  

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的元素需要定义为一个表单元素。ie输入类型,以便其工作。两者都使用了attr()和prop(),但是为了将来的可维护性,选择了后者。