将clickonly属性设置为false,以便在单击锚标记时输入html文本

我的HTML:

我的Javascript

   $(document).ready(function () { console.log("document ready") $("profileEdit label a").click( function (e) { if (this.attr("id") == "Aname") { $("#name").attr("readonly", false); } }); });  

替代Javascript

  $(document).ready(function () { console.log("document ready") $('#Aname').live('click', function () { $("#name").attr("readonly", false); }); });  

我想要做的是点击相应的锚点字段将相应输入文本字段的readonly属性设置为false。 我的javascript脚本都不起作用。

解决方案:合并@KaraokeStu后,@ bipin回答我使用的是asp.net 4.5

 $(document).ready(function () { console.log("document ready") $('.profileEdit label a').live('click', function () { alert("ctl00_ContentPlaceHolder1_" + this.id.substring(1, this.id.length)); $("#" + "ctl00_ContentPlaceHolder1_" + this.id.substring(1, this.id.length)).prop('readonly', false); console.log($("#" + "ctl00_ContentPlaceHolder1_" + this.id.substring(1, this.id.length)).attr('readonly')) $("#" + "ctl00_ContentPlaceHolder1_" + this.id.substring(1, this.id.length)).focus(); alert("done"); }); }); 

更改元素的readonly属性。使用prop()

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

链接以了解有关prop()和attr()的更多信息

readonly属性是一个布尔值。 您不能将其设置为truefalse ,您可以将其设置为readonly或根本不设置它( readonly=""错误,您可以将值保持关闭( readonly )或指定正确的值( readonly="readonly" ))。

如果要更改DOM中元素的只读状态,请将readonly 属性设置为truefalse 。 保留属性。

 $("#name").prop("readonly", false); 
   

你要么这样做:

 $(selector).attr('readonly', 'readonly'); $(selector).removeAttr('readonly'); 

或这个:

 $(selector).prop('readonly', true); $(selector).prop('readonly', false); 

永远不要混淆两者。

记忆并不难。 只记得在使用.attr()时,你正在处理属性值。 使用.prop()时,您正在处理DOM属性。

试试这个

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

看到这里。

工作演示

你可以不用jQuery来做到这一点:

设置readOnly

 document.getElementById("name").readOnly = true; 

并且未设置:

 document.getElementById("name").readOnly = false; 

一切都在纯香草JS 。

要将readonly设置为false,您需要从输入字段中完成删除属性:

 $("#name").removeAttr("readonly"); 
 $('div.profileEdit a').click(function() { // extract corresponding label id var labelId = $(this).attr('id').split('A')[1]; // make corresponding label readonly $('input#'+labelId).attr('readonly','readonly'); }); 

您可以使用以下代码:

  

正如这里所示: http : //jsfiddle.net/uDCgE/

set element property readonly="readonly"