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

thu*_*ird 10 javascript jquery

我的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>
Run Code Online (Sandbox Code Playgroud)

我的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>
Run Code Online (Sandbox Code Playgroud)

替代Javascript

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

我想要做的是在点击相应的锚点字段时将相应输入文本字段的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");
       });

    });
Run Code Online (Sandbox Code Playgroud)

bip*_*pen 28

更改元素的readonly属性.use prop()

 $("#name").prop('readonly', false);
Run Code Online (Sandbox Code Playgroud)

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


Que*_*tin 8

readonly属性是一个布尔值.您无法将其设置为true或者false,您可以将其设置为readonly或不设置它(readonly=""错误,您可以将值保留为off(readonly)或指定正确的值(readonly="readonly")).

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

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


sim*_*eco 6

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

readOnly

document.getElementById("name").readOnly = true;
Run Code Online (Sandbox Code Playgroud)

并取消设置:

document.getElementById("name").readOnly = false;
Run Code Online (Sandbox Code Playgroud)

一切都在纯Vanilla JS中。


Ter*_*ung 5

你要么这样做:

$(selector).attr('readonly', 'readonly');
$(selector).removeAttr('readonly');
Run Code Online (Sandbox Code Playgroud)

或这个:

$(selector).prop('readonly', true);
$(selector).prop('readonly', false);
Run Code Online (Sandbox Code Playgroud)

切勿将两者混为一谈。

不难记住。请记住,在使用 .attr() 时,您正在处理 Attribute 值。使用 .prop() 时,您正在处理 DOM 属性。