简体   繁体   English

在 IE6、IE7、IE8 中禁用按钮?

[英]Disabling a Button in IE6, IE7, IE8?

I am trying to disable a button - using jQuery 1.4.4 the following code in IE我正在尝试禁用按钮 - 在 IE 中使用 jQuery 1.4.4 以下代码

jQuery('#id').attr("disabled", true);

With the HTML of与 HTML 的

<button id="id" type="button"
    class="some-class"
     disabled="">Comment</button>

Works in FF, Chrome etc and of course, doesn't work in IE?在 FF、Chrome 等中工作,当然,在 IE 中不工作? How can I fix?我该如何解决?

ie the <button disabled="disabled"> doesn't seem to work in IE or?<button disabled="disabled">似乎在 IE 中不起作用,或者?

Edit: Note also that <button id='id' disabled>foobar</button> is valid html编辑:还要注意<button id='id' disabled>foobar</button>是有效的 html

XML/HTML attribute values are strings. XML/HTML 属性值是字符串。 The values "true" and "false" have no special meaning (technically, they aren't even allowed ) . "true" 和 "false" 值没有特殊含义(从技术上讲,它们甚至是不允许的) The convention is to set the value to the attribute name:约定是将值设置为属性名称:

jQuery('#id').attr("disabled", "disabled");

Also note that in your HTML, <button disabled=""> will already disable the button.另请注意,在您的 HTML 中, <button disabled="">已经禁用该按钮。 Just leave out the disabled attribute or re-enable it with jQuery:只需忽略disabled属性或使用 jQuery 重新启用它:

jQuery('#id').removeAttr("disabled");

I was having identical issues in IE8, and was able to solve them, so thought I'd add-in a couple points.我在 IE8 中遇到了相同的问题,并且能够解决它们,所以我想补充几点。 The solution offered by phihag is both right and wrong. phihag 提供的解决方案既对又错。 It's true that XML/HTML won't accept a true boolean value as an attribute--but we're talking about jQuery syntax, here, which does accept booleans as arguments, and sets attributes appropriately.确实,XML/HTML 不会接受真正的 boolean 值作为属性——但我们在这里讨论的是 jQuery 语法,它确实接受布尔值作为 arguments,并适当地设置属性。

With the disabling and enabling of input elements, in IE, what I found to work, consistently, is to simply not "hard-code" the initial desired value directly in the X/HTML.随着输入元素的禁用和启用,在 IE 中,我发现始终有效的方法是简单地直接在 X/HTML 中“硬编码”初始所需值。 If a control is to be disabled from the initial render, it's best to call a function as soon as the page is rendered, to disable it.如果要从初始呈现中禁用控件,最好在呈现页面后立即调用 function 以禁用它。 Perhaps a bit of a kludge and, like many things, it ought not be that way, but that's what's ended-up working for me.也许有点杂乱无章,就像许多事情一样,它不应该是那样的,但这就是最终为我工作的东西。 Very simple.很简单。

Hope that helps someone.希望对某人有所帮助。 I went through a lot of debugging efforts to pinpoint that one.我经历了很多调试工作来确定那个。

This is from JQuery API documentation :这是来自JQuery API 文档

$( ".selector" ).button( "option", "disabled", true );

It works for me even in IE 8 with JQuery UI 1.8.14 or higher.即使在带有 JQuery UI 1.8.14 或更高版本的 IE 8 中,它也适用于我。 In older versions the click events are fired even on disabled buttons.在旧版本中,即使在禁用按钮上也会触发点击事件。 So in the event, you have to evaluate disabledness of the button and cancel the event:因此,在这种情况下,您必须评估按钮的禁用情况并取消事件:

function clickHandler(event) {
    if ($(event.currentTarget).button("option", "disabled")) {
        return false;
    }
    // other handle stuff...
}

Try:尝试:

 jQuery('#id').attr("disabled", "disabled");

Attribute values should be strings, not boolean属性值应该是字符串,而不是 boolean

Just to add some extra value to the other answers posted here.只是为此处发布的其他答案添加一些额外的价值。

I think your wording is a bit off and by the looks of it, you want to know how to both disable and re-enable a button cross browser.我认为您的措辞有点偏离,从外观上看,您想知道如何禁用和重新启用按钮跨浏览器。

The disabled attribute, when present, should always disable the element, no matter what it's value is. disabled属性,当存在时,应该总是禁用元素,不管它的值是什么。 The recommended value is disabled="disabled" .推荐值为disabled="disabled"

$('#id').attr("disabled", "disabled")

In order to re-enable the element you'll want to remove the disabled attrbitute altogether (as pointed out in other answers).为了重新启用该元素,您需要完全删除disabled的属性(如其他答案中所指出的)。

$('#id').removeAttr('disabled');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM