Remove Readonly trong TextBox bằng js, javascript như thế nào

Thảo luận trong 'Lập trình Javascript, js' bắt đầu bởi seolagi, 8/8/18.

  1. seolagi
    Tham gia ngày:
    16/4/14
    Bài viết:
    1,027
    Đã được thích:
    80
    Điểm thành tích:
    48
    Remove Readonly trong TextBox bằng js, javascript như thế nào? mình có cái textbox như dưới, muốn khi người dùng lick vào button fa thì nó xóa đi, còn không thì mặc định nó sẽ là Readonly, mình sử dụng code C# asp.net mô hình MVC ý.

    Ở trên View
    HTML:
    <div class="row" style="position: relative;">
      @Html.TextBox("CateId", (string)ViewBag.CateName, new { @class = "form-control", @readonly = true })
      <i class="fa fa-minus-square removereadonly" style="position: absolute;top: 10px;right: 10px;font-size: 18px !important;"></i>
    </div>
    
    Khi hiển thị lên trình duyệt
    HTML:
    <div class="row" style="position: relative;">
        <input class="form-control" id="CateId" name="CateId" readonly="True" value="Trình dược Viên ETC,Trình dược Viên OTC,QC" type="text">
        <i class="fa fa-minus-square removereadonly" style="position: absolute;top: 10px;right: 10px;font-size: 18px !important;"></i>
    </div>
    
    • Mình muốn khi người dùng lick vào .removereadonly thì cái #CateId sẽ xóa readonly đi thành, có nghĩa là click vào mới cho sửa, không click thì không cho sửa ý.
    <input class="form-control" id="CateId" name="CateId" value="Trình dược Viên ETC,Trình dược Viên OTC,QC" type="text">
     
    Cảm ơn đã xem bài:

    Remove Readonly trong TextBox bằng js, javascript như thế nào

    admin thích bài này.
  2. admin
    Tham gia ngày:
    22/5/13
    Bài viết:
    4,883
    Đã được thích:
    1,193
    Điểm thành tích:
    113
    Giới tính:
    Nam
    Code JS của bạn đây nhé:
    Cách 1:
    HTML:
    <script>
    $(".removereadonly").click(function () {
           $("#CateId").removeAttr("readonly");
        });
    </script>
    
    Cách 2:
    HTML:
    <script>
    $(".removereadonly").click(function () {
    var btnbutton = $(this).val();
    if(btnbutton == "yes"){
             $("#no_of_staff").prop("readonly", false);
         }
         else{
             $("#no_of_staff").prop("readonly", true);
         }
    </script>
    
    Cách 3:
    HTML:
    <script>
    $(".removereadonly").click(function () {
    var btnbutton = $(this).val();
    if(btnbutton == "yes"){
             $("#no_of_staff").attr("readonly", false);
         }
         else{
             $("#no_of_staff").attr("readonly", true);
         }
    </script>
    
     


Chủ để tương tự : Remove Readonly
Diễn đàn Tiêu đề Date
Lập trình Javascript, js Add and Remove class Name javascript, thêm hoặc xóa class trong javascript 8/10/22
Lập trình Javascript, js Jquery remove all html from string, xóa tất cả ký tự html trong chuỗi như thế nào? 25/1/20
Lập trình Javascript, js Xóa remove height, width css trong jquery như thế nào ? 2/2/18