使用jquery禁用带有name属性的textarea字段

时间:2015-09-26 04:31:58

标签: jquery html

我有一个textarea,我想在满足某些条件时禁用它,否则它将被启用。

<textarea name='example'>I want to disable this</textarea>

我尝试过这种方法,但它不起作用:

$('#example').attr('disabled', true);

4 个答案:

答案 0 :(得分:4)

使用属性值选择器

禁用textarea

$('textarea[name="example"]').prop('disabled', true); // disable

启用

$('textarea[name="example"]').prop('disabled', false); // enable

<强>演示

&#13;
&#13;
$('#myButton').on('click', function() {
  var currentState = $(this).text();
  $('textarea[name="example"]').prop('disabled', currentState === 'Disable');
  $(this).text(currentState === 'Enable' ? 'Disable' : 'Enable');
});
&#13;
button {
  display: block;
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.0/jquery.min.js"></script>
<textarea name='example' disabled>I want to diable this</textarea>

<button id="myButton">Enable</button>
&#13;
&#13;
&#13;

答案 1 :(得分:1)

使用$("#textbox1").attr("disabled", "disabled");停用文本框。

<强>演示

HTML

<span id="radiobutt">
  <input type="radio" name="rad1" value="1" />
  <input type="radio" name="rad1" value="2" />
  <input type="radio" name="rad1" value="3" />
</span>
<div>
  <input type="text" id="textbox1" />
  <input type="checkbox" id="checkbox1" />
</div>

的Javascript

$("#radiobutt input[type=radio]").each(function(i){
  $(this).click(function () {
    if(i==2) { //3rd radiobutton
       $("#textbox1").attr("disabled", "disabled"); 
       $("#checkbox1").attr("disabled", "disabled"); 
    }
    else {
       $("#textbox1").removeAttr("disabled"); 
       $("#checkbox1").removeAttr("disabled"); 
    }
  });
});

答案 2 :(得分:0)

$('textarea[name="example"]').prop("disabled", true);

访问http://api.jquery.com/prop/

答案 3 :(得分:0)

你应该使用.prop()函数和属性值选择器

$('textarea[name="example"]').prop('disabled', true);
//$('textarea[name="example"]').prop('disabled', false);

注意:对于jQuery 1.6 +

$('textarea[name="example"]').attr("disabled","disabled");