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

このようにしてみてください:

<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>

Jクエリ:

 $('#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 に答える
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 に答える