larry*wei 2009-09-12 05:21 采纳率: 0%
浏览 306
已采纳

使用 jQuery 禁用 / 启用输入?

$input.disabled = true;

or

$input.disabled = "disabled";

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

转载于:https://stackoverflow.com/questions/1414365/disable-enable-an-input-with-jquery

  • 写回答

16条回答 默认 最新

  • 谁还没个明天 2009-09-12 05:23
    关注

    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 and below

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

    Set the disabled attribute.

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

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

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

    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;
    

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


    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:

    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.

    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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(15条)

报告相同问题?

悬赏问题

  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?