【问题标题】:jQuery button not respondingjQuery按钮没有响应
【发布时间】:2017-12-03 02:57:33
【问题描述】:

我正在尝试克隆一个表格行。这是行不通的。即使console.log(); 也不起作用。

我正在尝试克隆 'table-wrapper' div。

HTML

 <div class="table-wrapper">

  <tr>

      <td style="width: 300px">
        <select class="form-control" id="sel1">
        @foreach($products as $product)
          <option value="{{$product->id}}">{{$product->product_name}}</option>
        @endforeach
        </select>
      </td>

      <td style="width: 100px;">
        <input type="text" class="form-control" id="">
      </td>

      <td style="width: 300px">
        <select class="form-control" id="sel1">
          <option>Dag</option>
          <option>Week</option>
          <option>Weekend</option>
          <option>4</option>
        </select>
      </td>

      <td>
        <select class="form-control" id="sel1">
          <option>0</option>
          <option>6</option>
          <option>21</option>
        </select>
      </td>

      <td style="width: 150px;">
        <input type="text" class="form-control" id="">
      </td>

      <td style="padding-top: 20px;">
        <p>&euro;</p>
      </td>


  </tr>

JS 文件

$(document).ready(function() {
    console.log( "ready!" );

    $('#newcollection').click(function () {
      $(".table-wrapper:first").clone().insertAfter(".table-wrapper:last");
    });

    $('#newinput').click(function () {
      $(".rent-wrapper:first").clone().insertAfter(".rent-wrapper:last");
    });


});

按钮

<div class="col-lg-12">
  <input type="button" id="newcollection" class="btn btn-primary" value="+">
  </div>
</div>

你知道为什么这不起作用吗?正如我所说,甚至 console.log();不工作。

编辑:包含 JS 文件。它记录:页面加载时“准备就绪”,另一个按钮确实有效。

------------------[编辑]--------------- -----------------------------

我使用 bootstrap 的表示例创建了一个新表。即使有了这张新桌子,它也不起作用。当我单击按钮时,它不会触发任何东西。即使在控制台中,它也不会记录任何内容。 (“准备好了!”除外)

<div class="table-wrapper">
<table class="table table-striped">
    <thead>
      <tr>
        <th>Firstname</th>
        <th>Lastname</th>
        <th>Email</th>
      </tr>
    </thead>
    <tbody>
      <tr>
        <td>John</td>
        <td>Doe</td>
        <td>john@example.com</td>
      </tr>
      <tr>
        <td>Mary</td>
        <td>Moe</td>
        <td>mary@example.com</td>
      </tr>
      <tr>
        <td>July</td>
        <td>Dooley</td>
        <td>july@example.com</td>
      </tr>
    </tbody>
  </table>
</div>
<input type="button" id="newcollection" class="btn btn-primary" value="+">
</div>

包含的 JS 文件

<script src="{!! env('APP_URL') !!}/js/jquery-migrate-1.2.1.min.js"></script>
<script src="{!! env('APP_URL') !!}/js/bootstrap.min.js"></script>
<script src="{!! env('APP_URL') !!}/js/modernizr.min.js"></script>
<script src="{!! env('APP_URL') !!}/js/pace.min.js"></script>
<script src="{!! env('APP_URL') !!}/js/retina.min.js"></script>
<script src="{!! env('APP_URL') !!}/js/jquery.cookies.js"></script>
<script src="{!! env('APP_URL') !!}/js/main.js"></script> // This is the file i'm currently editting.

<script src="{!! env('APP_URL') !!}/js/jquery.sparkline.min.js"></script>
<script src="{!! env('APP_URL') !!}/js/morris.min.js"></script>
<script src="{!! env('APP_URL') !!}/js/raphael-2.1.0.min.js"></script>
<script src="{!! env('APP_URL') !!}/js/bootstrap-wizard.min.js"></script>
<script src="{!! env('APP_URL') !!}/js/select2.min.js"></script>

<script src="{!! env('APP_URL') !!}/js/custom.js"></script>

【问题讨论】:

  • 这可能是一个愚蠢的问题,但您是否将 JS 文件包含在 HTML 中?
  • 不是一个愚蠢的问题。我忘了说我已经在我的 html 文件中包含了我的 JS 文件。它之所以有效,是因为它在加载页面时确实记录了“就绪”。此外,另一个克隆按钮也可以工作。
  • $(".row-collection:first").clone() ...好吧,HTML 中的任何地方都没有 row-collection 类。所以这不会匹配任何东西。
  • @itvba:定义“不工作”。因为一旦你编辑了 jQuery 选择器,它就对我有用:jsfiddle.net/3ncfjx0u
  • 刚刚注意到,可能存在问题,因为您的标记无效并且行周围不包含&lt;table&gt; 标记。 David 的示例确实,并且是有效的 HTML。这可能是不同的。

标签: javascript jquery html html-table clone


【解决方案1】:

你错了

$(".table-wrapper:first")

您只想重复 tr,但您的目标是 .table-wrapper 类元素

$(document).ready(function() {
  console.log("ready!");

  $('#newcollection').click(function() {
    $(".table-wrapper tr").eq(0).clone().insertAfter(".table-wrapper tr:last");
  });


});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="table-wrapper">
  <table>
    <tr>

      <td style="width: 300px">

      </td>

      <td style="width: 100px;">
        <input type="text" class="form-control" id="">
      </td>

      <td style="width: 300px">
        <select class="form-control" id="sel1">
          <option>Dag</option>
          <option>Week</option>
          <option>Weekend</option>
          <option>4</option>
        </select>
      </td>

      <td>
        <select class="form-control" id="sel1">
          <option>0</option>
          <option>6</option>
          <option>21</option>
        </select>
      </td>

      <td style="width: 150px;">
        <input type="text" class="form-control" id="">
      </td>

      <td style="padding-top: 20px;">
        <p>&euro;</p>
      </td>


    </tr>
  </table>
  <div class="col-lg-12">
    <input type="button" id="newcollection" class="btn btn-primary" value="+">
  </div>
</div>

【讨论】:

  • 出了什么问题?你改变了什么?您的更改如何解决问题?只是转储代码并不能回答问题。
  • @David 我编辑了我的答案,这将有助于解决您的所有问题
  • 我已经更新了我的帖子。它仍然不起作用。你们可以检查我的编辑吗?我创建了一个新表,因为另一个表的结构有点乱。
  • @itvba,用这段代码更新我之前的代码。 $('#newcollection').click(function() { $(".table-striped tbody tr").eq(0).clone().insertAfter(".table-striped tbody tr:last"); });
  • $('#newcollection').click(function() { $(".table-striped tbody").append($(".table-striped tbody tr").eq(0).clone()); }); 两个代码都可以工作
猜你喜欢
  • 1970-01-01
  • 2019-06-24
  • 1970-01-01
  • 2015-01-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多