jjery点击事件在ajax事件后未触发

时间:2018-02-17 10:04:46

标签: javascript jquery ajax

在ajax调用后我无法点击按钮。它在通话前工作正常。但是,当我执行搜索并从php返回数据时,click事件将停止工作。

我尝试过.on事件,但这只是显示表单而不是值。如果有人能查看我的代码,看看我哪里出错,我将不胜感激。

我知道元素需要在页面中,我认为在他调用dj之后会包含元素。显然我错了。

非常感谢

点击次数。

1)页面加载,.edit点击工作正常。

2)从#btn-search执行搜索。 Pagintes the table但是.edit click不再触发。

js code

从行中获取初始值。在第一次点击时工作正常。不是在ajax调用后显示搜索结果

$(function() {
  $('.edit').click(function(e) {
    e.preventDefault();
    // code to read selected table row cell data (values).
    var currentRow = $(this).closest("tr");
    id = currentRow.find("td:eq(0)").html();
    service = currentRow.find("td:eq(1)").html();
    activity = currentRow.find("td:eq(2)").html();
    dept = currentRow.find("td:eq(3)").html();
    company = currentRow.find("td:eq(4)").html();
    address = currentRow.find("td:eq(5)").html();
    user = currentRow.find("td:eq(6)").html();
    item = currentRow.find("td:eq(7)").html();
    date = currentRow.find("td:eq(8)").html();
    var data = id + "\n" + service + "\n" + activity + "\n" + dept + "\n" + company + "\n" + address + "\n" + user + "\n" + item + "\n" + date;
    $("#editForm").dialog("open");
    $("#id").val(id);
    $("#service").val(service);
    $("#activity").val(activity);
    $("#dept").val(dept);
    $("#company").val(company);
    $("#address").val(address);
    $("#user").val(user);
    $("#item").val(item);
    $("#datetimepicker").val(date);
  });
});

php后端

<?php 
if ($_POST['search1'] == '') {
  echo 'enter a search term';
  exit;
} else {
  $search = $_POST['search1'];
  $sqlsrch = mysqli_query($conn, "select * from act where item like '%$search%'");
  $numsrch = mysqli_num_rows($sqlsrch);
  if ($numsrch == 0) {
    // what to do if no results found
    echo 'No Results Found';
  } else {
    while ($rowsrch = mysqli_fetch_array($sqlsrch)) {
      $id = $rowsrch['id'];
      $service = $rowsrch['service'];
      $activity = $rowsrch['activity'];
      $dept = $rowsrch['department'];
      $company = $rowsrch['company'];
      $address = $rowsrch['address'];
      $user = $rowsrch['user'];
      $box = $rowsrch['item'];
      $date = $rowsrch['date'];
      $date = date('d/m/Y h:i:s', strtotime($date));
      $edit = "<button type='button' class='btn btn-primary edit'>Edit</button>";
      $action = "<button type='button' class='btn btn-success action'>Action</button>";
      $delete = "<button type='button' class='btn btn-danger delete'>Delete</button>";
      // Now for each looped row
      echo "<tr><td>" . $id . "</td><td>" . $service . "</td><td>" . $activity . "</td><td>" . $dept . "</td><td>" . $company . "</td><td>" . $address . "</td><td>" . $user . "</td><td>" . $box . "</td><td>" . $date . "</td><td>" . $edit . ' ' . $action . ' ' . $delete . "</td></tr>";
    } // End our scale while loop
  }
}
?>

jquery搜索点击事件

$(function() {
  $('#btn-search').on('click', function(e) {
    e.preventDefault();
    var search = $("#search").val();
    $.ajax({
      url: "tabletestajax.php",
      method: 'POST',
      data: {
        search1: search
      },
      success: function(data) {
        $("#search").val('');
        $(".paginated tbody").empty();
        var result = $.trim(data);
        if (result === "enter a search term") {
          $("input[name='search']").val(result).css({
            'color': 'red'
          });
          return false;
        } else if (result === "No Results Found") {
          $(".paginated tbody").html('<div>' + result + '</div>');
          return false;
        } else {
          $(".paginated tbody").html(result);
        }
      }
    });
  });
});

1 个答案:

答案 0 :(得分:1)

可能你需要事件委托

$(document).on('click', '.edit', function(e) {
  e.preventDefault();
  // code to read selected table row cell data (values).
  var currentRow = $(this).closest("tr");
  id = currentRow.find("td:eq(0)").html();
  .
  .
}

资源