1

单击单选按钮时,我有如下代码是的,如果不是,则必须启用文本框,否则必须禁用它。请有人帮助我。谢谢

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

4 回答 4

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);
      }
 });
于 2013-07-26T06:46:08.710 回答
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);
}
于 2013-07-26T06:43:01.307 回答
1
if($('#high').is(":selected") {
   $('input').prop('disabled',true);
} else {
   $('input').prop('disabled',false);
}

尝试这个

于 2013-07-26T06:52:04.707 回答
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各自的 分配input给中的for属性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

于 2013-07-26T06:45:17.400 回答