dongtuota3633 2018-07-05 15:45
浏览 70
已采纳

在任何事件发生后,一旦它重新获得,就会使用ajax for table。

I am trying to make single page application with jquery using php i have created page which have 3 events inserts update and delete after each event data also be re fetched to display on table with new data but now the problem happening is when data be re fetched it not let events to work again like there are two button on every row of data edit and delete. but once you edit first row it not let you edit any other row. there must be way to make it but what could be solution i am newbie with jquery.

Here is my jquery.

$( document ).ready(function() {
  // When click the button.
$("#save").on('click',function() {

// Assigning Variables to Form Fields

var category  = $("#catname").val();

if(category){
      $.ajax({

     type: "post",
     url: "insert.php",
    asynch: false,
   data: {
    "category": category 

     },
     success: function(data){
          alert("Category Saved Successfully. ");
          $("#ecname").html("");
          $("#show").html(data);
        $( '#form_category' ).each(function(){
this.reset();


 });
     }
 });
}

else{
                     $("#ecname").html("fill category name");
}

});
  });

Here is html of table.

    <div class="m-portlet m-portlet--mobile">
                    <div class="row" id="categories">

                <?php

                        $i = 1;
$categories = $obj->getallcategories();
echo '
<div class="col-lg-12">
                            <div class="m-portlet__body">

                            <table class="table table-striped- table-bordered table-hover table-checkable" id="m_table_1">
                                <thead>
                                    <tr>
                                        <th>
                                        Id
                                        </th>
                                        <th>
                                            category Name
                                        </th>
                                        <th>
                                            category Name
                                        </th>
                                        <th>
                                            Edit
                                        </th>
                                        <th>
                                            Delete
                                        </th>

                                    </tr>
                                </thead>
                                <tbody id="show">
                            ';


foreach($categories as $category){
    echo'
    <tr>
    <td>'.$i.'</td><td>'.$category['cname'].'</td>
    <td>categories</td>
    <td><button value="'.$category['cid'].'" class="btn btn-primary get" id="get'.$category['cid'].'">edit </button></td>
    <td><button class="btn btn-danger" id="del'.$category['cid'].'" value="'.$category['cid'].'" type="button">Delete</button></td>
    </tr>
 ';
    $i++;
 }

                        ?>
                        </tbody>
                            </table>
                        </div>

                        </div>
                        </div>

Here is insert.php page which return data on ajax call of #save button click.

<?php

 include("../functions/backend.php");

 $obj = new Crud();




if(isset($_POST['category']))
 {


  $cname = $_POST['category'];
  $obj->addcategory($cname);

                $i = 1;
$categories = $obj->getallcategories();


foreach($categories as $category){
    echo'
        <tr>
    <td>'.$i.'</td><td>'.$category['cname'].'</td>
    <td>categories</td>
    <td><button value="'.$category['cid'].'" class="btn btn-primary get" id="get'.$category['cid'].'">edit </button></td>
    <td><button class="btn btn-danger" id="del'.$category['cid'].'" value="'.$category['cid'].'" type="button">Delete</button></td>
    </tr>
 ';
    $i++;
}



  }

 else {

echo "<script>alert('Something went wrong')</script>";
 }





 ?>

edited with new self function which i want to make delegate.

    function removeitem() {
        var cid = $("[id^=del]").val(); 
      $.ajax({

     type: "post",
     url: "delete.php",
    asynch: false,
   data: {

       "cid": cid

     },
     success: function(data){

             $("#show").html(data);
     }


   });



}


  $("[id^=del]").confirm({
text: "Are you sure you want to delete ?",

confirm: function(button) {

//removeitem();


},
cancel: function(button) {
    // nothing to do
},
confirmButton: "Yes I am",
cancelButton: "No",
post: true,
confirmButtonClass: "btn-danger",
cancelButtonClass: "btn-default",
dialogClass: "modal-dialog modal-lg" // Bootstrap classes for large modal
 });
  • 写回答

1条回答 默认 最新

  • douruhu4282 2018-07-05 16:32
    关注

    You need to read about event-delegation.

    Delegated event handlers have the advantage that they can process events from descendant elements that are added to the document at a later time. By picking an element that is guaranteed to be present at the time the delegated event handler is attached, you can use delegated events to avoid the need to frequently attach and remove event handlers.

    For example, you need to do it like this:

    $('body').on('click','#mybutton', function(){
       //code here..
    });
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥170 如图所示配置eNSP
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改
  • ¥20 wireshark抓不到vlan
  • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
  • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持
  • ¥15 stata安慰剂检验作图但是真实值不出现在图上