jQuery readonly 属性问题

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1420449/
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 11:25:15  来源:igfitidea点击:

jQuery readonly attribute problems

jquery

提问by ruturaj

I have this

我有这个

<input type="text" id="tbox" name="tbox" readonly="readonly" />

I have a button on which I do this

我有一个按钮可以执行此操作

$('#tbox').removeAttr('readonly');

I've also tried doing this

我也试过这样做

$('#tbox').attr('readonly', false);

.. but none work..

..但没有工作..

回答by Russ Cam

You will need to do this when the DOM has loaded using jQuery's ready event for the document object. Here's a Working Demo

当使用 jQuery 的文档对象的就绪事件加载 DOM 时,您将需要执行此操作。这是一个工作演示

$(document).ready(function() {

    $('#tbox').removeAttr('readonly');

});

or the shorthand

或速记

$(function() {

    $('#tbox').removeAttr('readonly');

});

EDIT:

编辑:

I just read on one of your other questionshow $()was not working but when you used jQuery(), your code worked. That indicates that there is a conflict with the $function, most likely due to another JavaScript framework being used on the page too that also uses the $shorthand. You can

我刚刚阅读了您的其他问题之一如何$()不起作用,但是当您使用时jQuery(),您的代码有效。这表明该$函数存在冲突,很可能是由于页面上使用的另一个 JavaScript 框架也使用了$简写。你可以

1- use jQuery's noConflict() to get around this. You can assign the jQuery selector function to a different alias.

1- 使用 jQuery 的 noConflict() 来解决这个问题。您可以将 jQuery 选择器函数分配给不同的别名。

2- usejQuery()in your code in place of $()

2-jQuery()在您的代码中使用代替$()

3- wrap your jQuery code in a self-invoking anonymous function that will still allow you to use the $()shorthand for the jQuery selector inside of it

3- 将您的 jQuery 代码包装在一个自调用匿名函数中,该函数仍然允许您在其中使用$()jQuery 选择器的简写

(function($) {

    $(function() {

        $('#tbox').removeAttr('readonly');

    });

})(jQuery);

This is an anonymous function that takes one parameter, $and is executed immediately, passing in jQueryas the argument for that parameter.

这是一个匿名函数,它接受一个参数,$并立即执行,jQuery作为该参数的参数传入。

回答by Natrium

read article on jetlogs

阅读关于jetlogs 的文章

The difference here is that they do

这里的不同之处在于他们做

<input type="text" id="tbox" name="tbox" readonly />

And then

进而

$('#tbox').removeAttr('readonly');

should work.

应该管用。

回答by JohnC

I know this is now Dec 2010, but I just read this post when searching on using JQuery to set and remove READONLY on a form's text box. I found and use this:

我知道现在是 2010 年 12 月,但我只是在搜索使用 JQuery 设置和删除表单文本框上的 READONLY 时阅读了这篇文章。我发现并使用了这个:

`$('#id-name').attr('readonly', true); // makes it readonly

`$('#id-name').attr('readonly', true); // 使其成为只读

$('#id-name').attr('readonly', false); // makes it editable`

$('#id-name').attr('readonly', false); // 使其可编辑`

回答by Shivam Srivastava

    <input type="submit"  id="btn1" onclick="setTimeout(disablefunction, 1);">

    add function of java script
        <script type="text/javascript" >


        function disablefunction() 
        {
            $('#btn1').attr('disabled',true)
        }


        </script>

I am sure this will work 100%

我相信这会 100%