2013-07-26 63 views
1

am在單選按鈕上單擊時具有以下代碼是如果不是必須禁用該文本框,則必須啓用該文本​​框。請有人幫助我。感謝如何使用jquery啓用文本區域或輸入type =「text」

<div class="label_left"> 
    <label>Does the tourism office operate on a biennial budget? : (Y/N)</label> 
</div> 
<div class="text_right"> 
    <br> 
    <br> 
    <input type="radio" id="high" name="high" /> 
    <label>Y</label>&nbsp;&nbsp;&nbsp;&nbsp; 
    <input type="radio" id="high" name="high" /> 
    <label>N</label> 
    <br /> 
    <br /> 
    <br /> 
</div> 
<div class="label_left"> 
    <label>If YES,please indicate when the current biennial budget began: (mm/dd/yy)</label> 
</div> 
<div class="text_right"> 
    <br> 
    <br> 
    <input type="text" name="date_biennial_budget" id="date_biennial_budget" value="" size="30" /> 
    <br /> 
    <br /> 
    <br /> 
</div> 
+0

[如何禁用jQuery的輸入?(HTTP的可能重複:// stackoverflow.com/questions/1414365/how-to-disable-an-input-with -jquery) – aBhijit

+0

檢查y答案發布非常簡短的代碼...! –

回答

6

首先,確保你的ID是唯一的..

使用prop()啓用/禁用元素..(確保您爲您的單選按鈕增加價值...在代碼中缺少)

HTML

<input type="radio" id="high" value="Y" name="high" /> 
<input type="radio" id="high" value="N" name="high" /> 

jQuery的

$('input[name="high"]').change(function(){ 
     if($(this).val() == 'Y'){ 
     $('#date_biennial_budget').prop('disabled',false); 
     }else{ 
     $('#date_biennial_budget').prop('disabled',true); 
     } 
}); 
+0

歡迎...快樂編碼 – bipen

3

它很容易與jQuery> 1.6

$('input').prop('disabled',true) 
$('input').prop('disabled',false) 

反應到你的單選按鈕,你必須給每一個不同的值屬性(1對嗎?),並聽取了change事件。

$("#date_biennial_budget").change(function(){ 

    if($(this).val() == 1) 
     $('input[name=high]').prop('disabled',false); 
    else 
     $('input[name=high]').prop('disabled',true); 
} 
+0

這將禁用整個'輸入'標籤包含收音機,按鈕等 – Praveen

+0

當然...編輯答案 –

0

使用該文本框,

$('input[type=text]').prop('disabled',true); 

更新: 既然你已經使用label標籤,您使用for屬性類似下面

<input type="radio" id="highY" name="high" /> 
<label for="highY">Y</label> 
<input type="radio" id="highN" name="high" /> 
<label for="highN">N</label> 

指定的id各自inputfor在此基礎上在label

屬性,你可以像這樣

$('input[name=high]').on('change', function() { 
    var label = $('label[for=' + this.id + ']').text(); 
    if (label === "Y") { 
     $('#date_biennial_budget').prop('disabled', false); 
    } else { 
     $('#date_biennial_budget').prop('disabled', true); 
    } 
}); 

JSFiddle

1
if($('#high').is(":selected") { 
    $('input').prop('disabled',true); 
} else { 
    $('input').prop('disabled',false); 
} 

試試這個

相關問題