簡體   English   中英

jQuery更改textarea以基於select進行讀寫

[英]jquery change textarea to read write based on select

我有一個文本區域,我想根據復選框將它從readyonly更改為readwrite。 在下面的jQuery中,我希望將textarea對象otherinjcomment設置為只讀,在選中otherinj時設置為readwrite,而在未選中otherinj時設置為readonly。

$(function(){
    $("#otherinj").change(function{
        If($(this).is(':checked')){
            $("#otherinjcomment").prop('readonly',False)
        } 
        else{
            $("#otherinjcomment").prop('readonly',True)   
        }
    })
})

這是HTML

<div id="afterdesciption">
    <label for="otherinj">Other(Specify below)</label>
    <input type="checkbox" id="otherinj" name="otherinj" value="1">
</div>

<div>
    <textarea name="otherinjcomment" readonly="readonly"></textarea>
</div>

要使其變為只讀然后再讀寫,您可以相應地切換disabled屬性,這是一個有效的示例鏈接 ,如果您有多個文本字段,則使用一個類作為選擇器,而不是一個ID

$("#otherinj").change(function(){
  if($(this).is(':checked')){
    $("#otherinjcomment").attr("disabled","disabled"); 
  } 
  else{
    $("#otherinjcomment").removeAttr('disabled')
  }
})

您的文本區域的名稱為“ otherinjcomment”,而不是id。 您需要以下jQuery代碼:

 $("#otherinj").change(function(){ if($(this).is(':checked')){ $("[name=otherinjcomment]").prop('readonly',false) } else{ $("[name=otherinjcomment]").prop('readonly',true) } }); 

$(“#otherinjcomment”)到$(“ [name = otherinjcomment]”)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM