Make textarea readonly with jquery

前端 未结 6 1710
醉梦人生
醉梦人生 2020-12-14 00:28

For text input I do:

$(\'input[type=\"text\"]\').each(function(){
  $(this).attr(\'readonly\',\'readonly\');
});

But what shou

相关标签:
6条回答
  • 2020-12-14 00:54

    You can write

    $("textarea").attr("readonly", "readonly");
    

    this will make readonly to all textarea fields.

    0 讨论(0)
  • 2020-12-14 00:57

    Try this

    $("#mytxtarea").attr("disabled", "disabled");
    
    0 讨论(0)
  • 2020-12-14 01:03

    http://www.w3schools.com/TAGS/att_textarea_readonly.asp
    How to select all textareas and textboxes using jQuery?

    0 讨论(0)
  • 2020-12-14 01:05

    Include it in your selector (using a multiple/element selector), like this:

    $('input[type="text"], textarea').attr('readonly','readonly');
    

    You can test it here, if it's the only thing you're doing, there's no need for a .each(), you can just call .attr() on all matched elements.

    0 讨论(0)
  • 2020-12-14 01:07

    In latest versions of jQuery, the use of method prop is preferred over use of attr.

    To make a particular textarea readonly:
    $('#mytextarea1').prop('readonly', true);

    To make all textareas readonly:
    $('textarea').prop('readonly', true);

    To make all 'text' fields readonly:
    $('input[type=text]').prop('readonly', true);

    To make all 'text' fields and textarea readonly:
    $('input[type=text],textarea').prop('readonly', true);

    Please also note the difference between 'readonly' and 'disabled' in terms of appearance:

    Below is a <textarea> with disabled set to true:
    Textarea disabled(looks different from a regular textarea)

    Below is a <textarea> with readonly set to true:
    textarea readonly(looks same as a regular textarea)

    0 讨论(0)
  • 2020-12-14 01:07

    From Jquery 1.6 use

    $("#mytxtarea").prop("disabled", true);
    

    Visit http://api.jquery.com/prop/

    0 讨论(0)
提交回复
热议问题