使用 jQuery 禁用/启用输入? [英] Disable/enable an input with jQuery?

查看:28
本文介绍了使用 jQuery 禁用/启用输入?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

$input.disabled = true;

$input.disabled = "disabled";

标准的方式是什么?而且,相反,您如何启用禁用的输入?

Which is the standard way? And, conversely, how do you enable a disabled input?

推荐答案

jQuery 1.6+

要更改 disabled 属性,您应该使用 .prop() 函数.

jQuery 1.6+

To change the disabled property you should use the .prop() function.

$("input").prop('disabled', true);
$("input").prop('disabled', false);

jQuery 1.5 及以下

.prop() 函数不存在,但是 .attr() 类似:

jQuery 1.5 and below

The .prop() function doesn't exist, but .attr() does similar:

设置禁用属性.

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

再次启用,正确的方法是使用.removeAttr()

To enable again, the proper method is to use .removeAttr()

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

在任何版本的 jQuery 中

你总是可以依赖实际的 DOM 对象,如果你只处理一个元素,它可能比其他两个选项快一点:

In any version of jQuery

You can always rely on the actual DOM object and is probably a little faster than the other two options if you are only dealing with one element:

// assuming an event handler thus 'this'
this.disabled = true;

使用 .prop().attr() 方法的优点是您可以为一组选定的项目设置属性.

The advantage to using the .prop() or .attr() methods is that you can set the property for a bunch of selected items.

注意:在 1.6 中有一个 .removeProp() 方法听起来很像 removeAttr(),但它不应该用于诸如 'disabled' 的本机属性的摘录文档:

Note: In 1.6 there is a .removeProp() method that sounds a lot like removeAttr(), but it SHOULD NOT BE USED on native properties like 'disabled' Excerpt from the documentation:

注意:请勿使用此方法删除本机属性,例如已选中、已禁用或已选中.这将完全删除该属性,一旦删除,将无法再次添加到元素中.使用 .prop() 将这些属性设置为 false.

Note: Do not use this method to remove native properties such as checked, disabled, or selected. This will remove the property completely and, once removed, cannot be added again to element. Use .prop() to set these properties to false instead.

事实上,我怀疑这个方法有很多合法的用途,布尔道具是以这样一种方式完成的,你应该将它们设置为 false 而不是像 1.5 中的属性"对应物那样删除"它们

In fact, I doubt there are many legitimate uses for this method, boolean props are done in such a way that you should set them to false instead of "removing" them like their "attribute" counterparts in 1.5

这篇关于使用 jQuery 禁用/启用输入?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆