jquery attr(readonly) function not working

匿名 (未验证) 提交于 2019-12-03 09:52:54

问题:

I am working with AST.Net and jQuery 1.8.3.

I want to make a textbox's readonly property to be true on pageload and false when a script runs. My code goes like this:

  function showdiv() {      $("#diviv").fadeIn("slow");     $("#txtname").attr('readonly', false);     $("#txtmobile").attr('readonly', false);     $("#txtemail").attr('readonly', false); } 

I want to make the readonly property of these three textboxes to be false when the function showdiv runs. Also I am making a div visible when the script runs. But it's not making it false. Please give me some suggessions how to make it run and tell me where the problem is.

回答1:

Readonly is a boolean attribute, which means that if it is present, no matter what value it has, it will be set. Since attributes are always strings, you are setting it to "false". Use prop instead.

   $("#txtname").prop('readonly', false); 


回答2:

try this

$("#txtmobile").prop('readonly', false); 

OR

$("#<%= txtname.ClientID %>").prop('readonly', false); 


回答3:

readonly is a boolean attribute, which means that if it is present, no matter what value it has, it will be set. Since attributes are always strings, you are setting it to "false". Use prop instead.

$("#txtname").prop('readonly', false); 


回答4:

You just can use removeAttr in jquery.

 $("#txtmobile").removeAttr('readonly'); 


回答5:

The readonly property of a textbox is does not take true/false as values, all you have to specify is the keyword readonly like so,

<input type="text" id="txtName" readonly /> 

Now, to "disable" this property, you will have to remove this using jQuery's .removeAttr() method,

$("#txtName").removeAttr("readonly"); 

test link



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!