javascript – 在单击锚标记时为html文本输入设置readonly属性为false

前端之家收集整理的这篇文章主要介绍了javascript – 在单击锚标记时为html文本输入设置readonly属性为false前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我的 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>

我的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>

替代Javascript

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

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

link阅读更多关于prop()和attr()的信息

原文链接:https://www.f2er.com/js/156837.html

猜你在找的JavaScript相关文章