2013-07-26 59 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); 
} 

试试这个

相关问题