【问题标题】:Editable bootstrap table row on button click using jquery使用jquery单击按钮时可编辑的引导表行
【发布时间】:2019-04-24 19:29:42
【问题描述】:

我正在尝试使用 Jquery 在单击按钮时编辑引导表的值。我参考了这篇文章making row editable 它工作得很好。我唯一的问题是双击按钮进入编辑模式。这是我的代码 sn-p

$('.editbtn').click(function () {
              var currentTD = $(this).parents('tr').find('td');
              if ($(this).html() == 'Edit') {
                  currentTD = $(this).parents('tr').find($("td").not(":nth-child(1)"));
                  $.each(currentTD, function () {
                      $(this).prop('contenteditable', true).css({
                        'background':'#fff',
                        'color':'#000'

                    })
                  });
              } else {
                 $.each(currentTD, function () {
                      $(this).prop('contenteditable', false).removeAttr("style");
                  });
              }
    
              $(this).html($(this).html() == 'Edit' ? 'Save' : 'Edit')
              if ($(this).html() == 'Save'){
                $(this).prop('contenteditable',false)
              }
    
          });
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" rel="stylesheet"/>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js"></script>
<table id="tableone" class="table table-striped table-dark table-bordered" border="1">
        <thead>
            <tr>
                <th scope="col">Date</th>
                  <th scope="col">Name</th>

                  <th scope="col">Status</th>
            </tr>
        </thead>
       
<tbody>
               <tr>
                  <td>21st August</td>
                  <td>abc</td>
                 
                       <td  contenteditable="false"><button type="button"class="btn btn-primary editbtn">Edit</button>
                    <button type="button" class="btn btn-danger">
                    Delete
                     </button></td>
               </tr>
            <tr>
                  <td>21st August</td>
                  <td>abc</td>
             
                      <td  contenteditable="false"><button type="button" class="btn btn-primary editbtn" >
                     Edit
                     </button>
                    <button type="button" class="btn btn-danger">
                    Delete
                     </button></td>
               </tr>  
               <tr>
                  <td>21st August</td>
                  <td>abc</td>
         
                    <td  contenteditable="false"><button type="button" class="btn btn-primary editbtn" >
                     Edit
                     </button>
                    <button type="button" class="btn btn-danger">
                    Delete
                     </button></td>
               </tr>  <tr>
                  <td>21st August</td>
                  <td>abc</td>
              

                  <td  contenteditable="false"><button type="button" class="btn btn-primary editbtn">
                     Edit
                     </button>
                    <button type="button" class="btn btn-danger">
                    Delete
                     </button></td>
               </tr>
            </tbody>
    </table>

这里我禁用了编辑一列中的数据。如您所见,当我们单击第一行中的编辑按钮时,只需单击一次,但对于其余行,则需要多次单击。多次单击后,它会在单击编辑模式,直到刷新页面。我不知道我错过了哪里。我会感谢你的帮助。

【问题讨论】:

    标签: jquery html bootstrap-4 bootstrap-table


    【解决方案1】:

    问题在于这一行:它考虑了 line,而您却考虑了 $(this).html()

    <button type="button" class="btn btn-primary editbtn" >
                         Edit
                         </button>
    

    为什么它在下次点击时起作用,因为在您的代码中您更改为 Edit

    $(this).html($(this).html() == 'Edit' ? 'Save' : 'Edit')
    

    您可以尝试以下操作:

    使用$(this).text().trim() 或更正该行:

    <button type="button" class="btn btn-primary editbtn" >Edit</button>
    

    <!DOCTYPE html>
    <html lang="en">
    <head>
      <title>Bootstrap Example</title>
      <meta charset="utf-8">
      <meta name="viewport" content="width=device-width, initial-scale=1">
      <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css">
      <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
      <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js"></script>
      <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js"></script>
      <script>
      $(document).ready(function(){
      $('.editbtn').click(function () {
                  var currentTD = $(this).parents('tr').find('td');
                  //alert('this '+$(this).html() );
                  if ($(this).text().trim() == 'Edit') {
                      currentTD = $(this).parents('tr').find($("td").not(":nth-child(1)"));
                      //alert("first if "+currentTD.html());
                      $.each(currentTD, function () {
                          $(this).prop('contenteditable', true).css({
                            'background':'#fff',
                            'color':'#000'
    
                        })
                      });
                  } else {
                   // alert("second if "+currentTD.html());
                     $.each(currentTD, function () {
                        //alert("second if "+currentTD.html());
                          $(this).prop('contenteditable', false).removeAttr("style");
                      });
                  }
        
                  $(this).html($(this).html() == 'Edit' ? 'Save' : 'Edit')
                  if ($(this).html() == 'Save'){
                   // alert("third if "+currentTD.html());
                    $(this).prop('contenteditable',false)
                  }
        
              });
              });
      </script>
    </head>
    <body>
    <table id="tableone" class="table table-striped table-dark table-bordered" border="1">
            <thead>
                <tr>
                    <th scope="col">Date</th>
                      <th scope="col">Name</th>
    
                      <th scope="col">Status</th>
                </tr>
            </thead>
           
    <tbody>
                   <tr>
                      <td>21st August</td>
                      <td>abc</td>
                     
                           <td  contenteditable="false"><button type="button"class="btn btn-primary editbtn">Edit</button>
                        <button type="button" class="btn btn-danger">
                        Delete
                         </button></td>
                   </tr>
                <tr>
                      <td>21st August</td>
                      <td>abc</td>
                 
                          <td  contenteditable="false"><button type="button" class="btn btn-primary editbtn" >Edit</button>
                        <button type="button" class="btn btn-danger">
                        Delete
                         </button></td>
                   </tr>  
                   <tr>
                      <td>21st August</td>
                      <td>abc</td>
             
                        <td  contenteditable="false"><button type="button" class="btn btn-primary editbtn" >Edit</button>
                        <button type="button" class="btn btn-danger">
                        Delete
                         </button></td>
                   </tr>  <tr>
                      <td>21st August</td>
                      <td>abc</td>
                  
    
                      <td  contenteditable="false"><button type="button" class="btn btn-primary editbtn">Edit</button>
                        <button type="button" class="btn btn-danger">
                        Delete
                         </button></td>
                   </tr>
                </tbody>
        </table>
    </body>
    </html>

    【讨论】:

      猜你喜欢
      • 2015-12-06
      • 1970-01-01
      • 2016-01-19
      • 1970-01-01
      • 2014-06-27
      • 1970-01-01
      • 1970-01-01
      • 2012-04-29
      • 2021-01-22
      相关资源
      最近更新 更多