How to disable a text area?

て烟熏妆下的殇ゞ 提交于 2019-12-21 12:12:00

问题


How to disable a textarea which is dynamically added to the HTML?

HTML:

<div class="ss_datesel_inp_cont">
    <div class="ss_datesel_inp_right_corner">
    </div>
    <input autocomplete="off">
</div>

This is what I tried:

   $('.ss_datesel_inp_cont:textarea').prop("disabled", true);
   $('.ss_datesel_inp_cont:input').prop("disabled", true);

回答1:


I don't see a textarea element in the code you posted? Based on the markup you have, you would disable the input by:

$('.ss_datesel_inp_cont input').prop('disabled', true);

The reason your code isn't working is simply because the selector is wrong. The input is a descendant of .ss_datesel_inp_cont and therefore you need to indicate that with a space between .ss_datesel_inp_cont and input. The input is also a direct child, so alternatively you could use the direct child token > as in $('.ss_datesel_inp_cont > input').




回答2:


To change the disabled property you should use the .prop() function.

$("textarea").prop('disabled', true);
$("textarea").prop('disabled', false);

Note: For jQuery 1.6+




回答3:


Have you tried,

$('.ss_datesel_inp_cont:textarea').attr("disabled", true);

prop only works for jQuery 1.6 or later.




回答4:


I would try:

$('.ss_datesel_inp_cont:textarea').prop("disabled", "disabled");

or

$('.ss_datesel_inp_cont textarea').prop("disabled", "disabled");



回答5:


You can do this:

$('.ss_datesel_inp_cont input').attr('disabled','disabled');

Your input is within the div, so you can select it in the same way as you select with css, by a space.

It is a child of the div, so you could also do this:

.ss_datesel_inp_cont > input

Example: http://jsfiddle.net/UpHsA/



来源:https://stackoverflow.com/questions/7694694/how-to-disable-a-text-area

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!