0

我有以下 HTML 表格。

<table id="sample_editable_1" role="grid" aria describedby="sample_editable_1_info">
    <tbody>
        <tr role="row" class="odd">
            <td class="sorting_1">
                <input type="text" class="form-control" value=""> </td>
            <td>
                <input type="text" class="form-control" value=""> </td>
            <td>
                <input type="text" class="form-control" value=""> </td>
            <td class="center">
                <button id="sample_editable_1_new" class="btn green"> <i class="fa fa-plus"></i> </button>
            </td>
        </tr>
    </tbody>
</table>

当我单击“btn green”类的按钮时,我通过使用委托事件处理程序生成一个新行,如下所示。

$('#sample_editable_1').on('click', ".btn green", function() {
    $(".odd:last").after('<<tr role="row" class="odd"><td class="sorting_1"> <input type="text" class="form-control" value=""> </td><td> <input type="text" class="form-control" value=""> </td><td> <input type="text" class="form-control" value=""> </td><td class="center"><button id="sample_editable_1_new" class="btn green"> <i class="fa fa-plus"></i> </button></td></tr>');
});

但是,当我创建一个新行时,我需要上一行的“添加新”按钮消失。我可以在 jQuery 中使用 hide() 函数,但是如何访问上一行?以下不起作用。

$( row ).prev()
4

4 回答 4

1

您可以使用上下文hide()单击的按钮。this此外,您的选择器不正确,它应该".btn.green"代替".btn green"

$('#sample_editable_1').on('click', ".btn.green", function() {
  $(this).hide();
  //Rest of the code
});

    $('#sample_editable_1').on('click', ".btn.green", function() {
      $(this).hide();
      $(".odd:last").after('<tr role="row" class="odd"><td class="sorting_1"> <input type="text" class="form-control" value=""> </td><td> <input type="text" class="form-control" value=""> </td><td> <input type="text" class="form-control" value=""> </td><td class="center"><button id="sample_editable_1_new" class="btn green"> <i class="fa fa-plus"></i> Add</button></td></tr>');
    });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table id="sample_editable_1" role="grid" aria describedby="sample_editable_1_info">
  <tbody>
    <tr role="row" class="odd">
      <td class="sorting_1">
        <input type="text" class="form-control" value="">
      </td>
      <td>
        <input type="text" class="form-control" value="">
      </td>
      <td>
        <input type="text" class="form-control" value="">
      </td>
      <td class="center">
        <button id="sample_editable_1_new" class="btn green"> <i class="fa fa-plus">Add</i> 
        </button>
      </td>
    </tr>
  </tbody>
</table>

于 2016-01-29T06:40:03.143 回答
1

你不能隐藏刚刚被点击的按钮吗?

    $('#sample_editable_1').on('click', ".btn green", function() {
        $(".odd:last").after('<<tr role="row" class="odd"><td class="sorting_1"> <input type="text" class="form-control" value=""> </td><td> <input type="text" class="form-control" value=""> </td><td> <input type="text" class="form-control" value=""> </td><td class="center"><button id="sample_editable_1_new" class="btn green"> <i class="fa fa-plus"></i> </button></td></tr>');
        $(this).hide();
    });
于 2016-01-29T06:40:52.327 回答
0

我发现了一些东西,你可以试试这个吗,更多阅读这个

$("#addrows").click(function () {
     $("#mytable").each(function () {
         var tds = '<tr>';
         jQuery.each($('tr:last td', this), function () {
             tds += '<td>' + $(this).html() + '</td>';
         });
         tds += '</tr>';
         if ($('tbody', this).length > 0) {
             $('tbody', this).append(tds);
         } else {
             $(this).append(tds);
         }
     });
});
于 2016-01-29T06:41:33.173 回答
0

试试这样:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table id="sample_editable_1" role="grid" aria describedby="sample_editable_1_info">
<tbody>
<tr role="row" class="odd">
  <td class="sorting_1">
    <input type="text" class="form-control" value="">
  </td>
  <td>
    <input type="text" class="form-control" value="">
  </td>
  <td>
    <input type="text" class="form-control" value="">
  </td>
  <td class="center">
    <button id="sample_editable_1_new" class="btn green"> <i class="fa fa- plus">Add</i> 
    </button>
  </td>
</tr>
</tbody>
</table>

查询:

 $('#sample_editable_1').on('click', ".btn.green", function() {
  $(this).hide();
  $(".odd:last").after('<tr role="row" class="odd"><td class="sorting_1"> <input type="text" class="form-control" value=""> </td><td> <input type="text" class="form-control" value=""> </td><td> <input type="text" class="form-control" value=""> </td><td class="center"><button id="sample_editable_1_new" class="btn green"> <i class="fa fa-plus"></i> Add</button></td></tr>');
});

jsfiddle:https ://fiddle.jshell.net/f4ux0bcs/

于 2016-01-29T07:02:38.770 回答