2013-04-30 5 views
6

MY HTMLクリックでHTMLテキスト入力のためのfalseにReadOnlyプロパティを設定します。は、アンカータグ

<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"); 
     }); 

    }); 
+0

リクエストを明確にしてください。あなたの質問の一番下の行が「偽に設定されています」と表示されている間、質問のタイトルは「trueに設定」を読み取ります。 –

答えて

19

変更を参照してください。この

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

をお試しください読取り専用(小道具についての詳細を読むためにelement..use prop()

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

linkのTY)とのattr()

+0

これは機能しました。ありがとう! – thunderbird

+0

ようこそ...うれしい...幸せなコード – bipen

+0

#nameはプロパティの名前かIDですか? – Cullub

7

readonly属性はブール値です。 trueまたはfalseに設定できない場合は、readonlyに設定するか、まったく設定しないでください(readonly=""が間違っている場合は、値をオフ(readonly)のままにするか、正しい値(readonly="readonly")を指定します。

あなたはDOMの要素の読み取り専用ステータスを変更したい場合は、trueまたはfalsereadonlyプロパティを設定します。属性はそのままにしておきます。

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

falseにreadonlyを設定するためには、あなたが入力フィールドから属性を削除完了する必要があります。

あなたは以下のコードを使用することができます
$("#name").removeAttr("readonly"); 
+1

そして、downvoteの理由は? –

+0

はい 'prop()'は 'attr()'より優先されるべきですか?実際には良いアイデア – thunderbird

0
$('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'); 
}); 
0

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

ここに示すように: http://jsfiddle.net/uDCgE/

+0

。あなたのコードはクリックを待っていませんでした。 'ctl00_ContentPlaceHolder1_" '文字列を' ClientId'がjavascript文字列で動作するようにidの先頭に追加しなければならなかったので、asp.netを使用しているので 'each(function(e){'を削除する必要があります。 – thunderbird

+0

クライアント側とサーバー側のコードを混在させると面白いことがありますか? – KaraokeStu

+0

これはあなたと@bipen answers.asp.netを組み合わせた後に到着したものです。すべてのrunat = "server"吸う。 – thunderbird

3

あなたはこれを行うのいずれか:

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

またはこの:

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

決して 2をミックスし。

覚えにくいです。 .attr()を使用するときは、属性値を扱っていることを覚えておいてください。 .prop()を使用するときは、DOMプロパティを扱っています。

-1

セット要素プロパティreadonly="readonly"

1

あなたはjQueryのせずにそれを行うことができます。

readOnlyを設定します。

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

と解除:純粋Vanilla JS

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

すべてを。

関連する問題