简体   繁体   English

单击锚标记时,将 HTML 文本输入的 readonly 属性设置为 false

[英]Set readonly property to false for an HTML text input on clicking an anchor tag

My HTML:我的 HTML:

<div class="profileForm">
    <fieldset>
    <label>Name<input type="text" id="name" name="name" runat="server" readonly=""/></label>
    <label>Email<input type="email" id="email" name="email" runat="server" readonly=""/></label>
    <label>Date Of Birth<input type="date" id="dob" name="dob" runat="server" readonly=""/></label>
    <label>Address<input type="text" id="address" name="address" runat="server" readonly=""/></label>
    <label>City<input type="text" id="city" name="city" runat="server" readonly=""/></label>
    <label>State<input type="text" id="state" name="state" runat="server" readonly=""/></label>
    <label>Country<input type="text" id="country" name="country" runat="server" readonly=""/></label>
    <label>Access Level<input type="text" id="accessLevel" name="accessLevel" runat="server" readonly=""/></label>
    </fieldset>
</div>
<div class="profileEdit">
    <fieldset>
        <label><a href="#" id="Aname">edit</a></label>
        <label><a href="#" id="Aemail">edit</a></label>
        <label><a href="#" id="Adob">edit</a></label>
        <label><a href="#" id="Aaddress">edit</a></label>
        <label><a href="#" id="Acity">edit</a></label>
        <label><a href="#" id="Astate">edit</a></label>
        <label><a href="#" id="Acountry">edit</a></label>
    </fieldset>
</div>

My JavaScript我的 JavaScript

<script src="Scripts/jquery-1.7.1.js"></script>
<script type="text/javascript">
    $(document).ready(function () {
        console.log("document ready")
        $("profileEdit label a").click(
        function (e) {
            if (this.attr("id") == "Aname") {
                $("#name").attr("readonly", false);
            }
        });
    });
</script>

Alternate JavaScript替代 JavaScript

<script type="text/javascript">
    $(document).ready(function () {
        console.log("document ready")
        $('#Aname').live('click', function () {
            $("#name").attr("readonly", false);
        });
    });
</script>

What I am trying to do is set readonly attribute of the corresponding input text field to false on click of the corresponding anchor field.我想要做的是在单击相应的锚点字段时将相应输入文本字段的readonly属性设置为 false 。 None of my JavaScript scripts works.我的 JavaScript 脚本都不起作用。

Solution: after combining @KaraokeStu, @bipin answers I am using asp.net 4.5解决方案:结合@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");
       });

    });

change the readonly property of an element..use prop()更改元素的只读属性..使用prop()

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

link to read more about prop() and attr() 链接以阅读有关 prop() 和 attr() 的更多信息

The readonly attribute is a boolean. readonly属性是一个布尔值。 You can't set it to true or false , you can set it to readonly or not set it at all ( readonly="" is wrong, you can leave the value off ( readonly ) or specify the correct value ( readonly="readonly" )).您不能将其设置为truefalse ,您可以将其设置为readonly或根本不设置它( readonly=""是错误的,您可以关闭该值( readonly )或指定正确的值( readonly="readonly" ))。

If you want to change the readonly status of an element in the DOM, then set the readonly property to true or false .如果要更改 DOM 中元素的只读状态,请将readonly属性设置为truefalse Leave the attribute alone.单独保留属性。

 $("#name").prop("readonly", false);
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <input id=name readonly>

You either do this:你要么这样做:

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

Or this:或这个:

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

Never mix the two.切勿将两者混为一谈。

It's not hard to memorize.不难记住。 Just remember when using .attr(), you're dealing with Attribute values.请记住,在使用 .attr() 时,您正在处理 Attribute 值。 When using .prop(), you're dealing with the DOM properties.使用 .prop() 时,您正在处理 DOM 属性。

You can do it without jQuery:你可以在没有 jQuery 的情况下做到:

Set readOnly :设置readOnly

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

and unset:并取消设置:

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

Everything in pure Vanilla JS .一切都在纯香草 JS 中

Try this试试这个

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

See here.看到这里。

WORKING DEMO工作演示

为了将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');
});

You can use the following code:您可以使用以下代码:

<script>
$(document).ready(function () {
    $(".profileEdit label a").each(function (e) {
        $(this).click(function (elem) {
           $("#" + this.id.substring(1,this.id.length)).attr("readonly", false); 
        });
    });    
});
</script>

As demonstrated here: http://jsfiddle.net/uDCgE/如此处所示: http : //jsfiddle.net/uDCgE/

Along with the provided answer, in order to complete the description of the readonly attribute, as a boolean attribute , would like to mention that, contrarily to the terminology used in classicel programming languages, where a boolean is a variable able to have two states: true or false, W3C defines a boolean attribute in the following way :除了提供的答案,为了完成readonly属性的描述,作为一个boolean 属性,想提一下,与经典编程语言中使用的术语相反,其中一个 boolean 是一个能够有两种状态的变量:无论是真还是假,W3C 都通过以下方式定义了一个布尔属性:

The presence of a boolean attribute on an element represents the true value, and the absence of the attribute represents the false value.元素上布尔属性存在表示true值,不存在该属性表示false值。

If the attribute is present, its value must either be the empty string or a value that is an ASCII case-insensitive match for the attribute's canonical name, with no leading or trailing whitespace.如果该属性存在,则其值必须是空字符串或与该属性的规范名称不区分大小写的 ASCII 匹配值,且没有前导或尾随空格。

Note: The values "true" and "false" are not allowed on boolean attributes.注意:布尔属性不允许使用值“true”和“false”。 To represent a false value, the attribute has to be omitted altogether.要表示假值,必须完全省略该属性。

So, the jQuery所以,jQuery

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

is only a syntax to remove the readonly attribute只是删除readonly 属性的语法

document.getElementById("demo").removeAttribute("readonly");

any other approach, like setting it to false or another value (like 0 ) will not 'eable' the input...任何其他方法,例如将其设置为false或其他值(例如0 )都不会“启用”输入...

设置元素属性readonly="readonly"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM