dongmo8943 2017-09-21 07:22
浏览 76
已采纳

在更改和页面加载上运行jQuery

I want to run jQuery when select element change and when page load. In this web page I have two table which user can move row from tbl1 to table2 using jQuery and every thing work fine but when I run the jQuery when select change and page load I can not move row from one table to another this is the code which I wish to run when page load and select is change

<script>
    $(document).ready(function () {

        $('#Groups').change(function () {

// show that something is loading
//$('#result').html('<img src="images/ajax_loader_red_128.gif"/>')

            var jtarih = $('#Groups').val();

//  alert(jtarih);
//var jadsoyad=$('#adsoyad').val();


            /*
             * 'post_receiver.php' - where you will pass the form data
             * $(this).serialize() - to easily read form data
             * function(data){... - data contains the response from post_receiver.php
             */

            $.post('groupuser.php', {id: jtarih, tarih: jtarih}, function (data) {
                $('#dive').html(data);

// show the response
//    $('#dive').replaceWith(html(data));

            }).fail(function (data) {

// just in case posting your form failed
                $('#dive').html(data);

            });


// to prevent refreshing the whole page page
            return false;

        });
        $('#Groups').trigger('change');
    });
</script>

This is the JavaScript code which move row from one table to another in this code every thing go fine but when I run the above JavaScript code onload page this code not working

<script>
    $(document).ready(function () {
        $("#product-table2 img.move-row").live("click", function () {
            var tr = $(this).closest("tr").remove().clone();
            tr.find("img.move-row")
                    .attr("src", "images/arrow.png")
                    .attr("alt", "Move");
            $("#product-table tbody").append(tr);
        });

        $("#product-table img.move-row").live("click", function () {
            var tr = $(this).closest("tr").remove().clone();
            tr.find("img.move-row")
                    .attr("src", "images/leftarrow.png")
                    .attr("alt", "Remove");
            $("#product-table2  tbody").append(tr);
        });
    });

</script>
  • 写回答

2条回答 默认 最新

  • dongque20030402 2017-09-21 08:01
    关注

    you are adding that code in the onload function, which means that by completing the page load it will stop working. You should leave the function off, and call it also inside the onload.

    <script>
       $(document).ready(function (){
       mover_fila();
       });
    
       mover_fila();
    
       function mover_fila(){
           $('#Groups').change(function () {
               var jtarih = $('#Groups').val();
    
               $.post('groupuser.php', {id: jtarih, tarih: jtarih}, function (data) {
               $('#dive').html(data);
    
           }).fail(function (data) {
           $('#dive').html(data);
       });
    
       return false;
    
       });
       $('#Groups').trigger('change');
    }
    </script>
    

    This is the second code, check its functionality with your html. In theory it should work if you remove your code from the onload function.

    <script>
       $(document).ready(function (){
          mover_otra_fila();
       });
    
       mover_otra_fila();
    
       function mover_otra_fila(){
           $("#product-table2 img.move-row").live("click", function () {
               var tr = $(this).closest("tr").remove().clone();
               tr.find("img.move-row")
                       .attr("src", "images/arrow.png")
                       .attr("alt", "Move");
               $("#product-table tbody").append(tr);
           });
    
           $("#product-table img.move-row").live("click", function () {
               var tr = $(this).closest("tr").remove().clone();
               tr.find("img.move-row")
                       .attr("src", "images/leftarrow.png")
                       .attr("alt", "Remove");
               $("#product-table2  tbody").append(tr);
           });
       }
    </script>
    

    Good luck, I hope it's useful.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥30 VMware 云桌面水印如何添加
  • ¥15 用ns3仿真出5G核心网网元
  • ¥15 matlab答疑 关于海上风电的爬坡事件检测
  • ¥88 python部署量化回测异常问题
  • ¥30 酬劳2w元求合作写文章
  • ¥15 在现有系统基础上增加功能
  • ¥15 远程桌面文档内容复制粘贴,格式会变化
  • ¥15 这种微信登录授权 谁可以做啊
  • ¥15 请问我该如何添加自己的数据去运行蚁群算法代码
  • ¥20 用HslCommunication 连接欧姆龙 plc有时会连接失败。报异常为“未知错误”