YaoRaoLov 2016-04-14 12:11 采纳率: 50%
浏览 43

$ .ajax在JQuery中调用相同的VM

Code in index.php:

<!DOCTYPE html>
<html lang="pt-br">
    <head>
        <title></title>
        <script type="text/javascript" src="jquery-2.2.3.min.js"></script>
        <script>
            function carregaPagina(item, pai, filho, url) {
                var caminho = item != null ? $(item).attr('data-click') : url;
                if (item != null) {
                    if ($(item).attr('data-json') != null) {
                        $('#' + $(item).attr('local-json')).html($(item).attr('data-json'));
                    }
                }
                if (pai != "" && pai != null) {
                    $('#' + pai).slideToggle(1000);
                }
                $.ajax({
                    url: caminho,
                    cache: false,
                    dataType: "html",
                    success: function (data) {
                        $('#' + filho).slideToggle(0);
                        $('#' + filho).html(data);
                        $('#' + filho).slideToggle(1000, function () {
                            if (typeof completaLoad !== 'undefined' && $.isFunction(completaLoad)) {
                                completaLoad();
                            }

                            if ($('#' + pai).css('display') !== 'none') {
                                $('#' + pai).slideToggle(0);
                            }
                        });
                    }
                });
                return false;
            }

            $(document).ready(function () {
                $(document).on('click', '.grid', function (e) {
                    if (e.handled !== true) {
                        carregaPagina(this, 'TelaConsulta', 'TelaEditar', null);
                        e.handled = true;
                    }
                    return false;
                });
            });
        </script>
        <style>
            .grid, .item, .btnVoltar {
                cursor: pointer;
            }
            #TelaConsulta {
                background-color: #81BEF7;
            }
            #TelaEditar {
                background-color: #58FAAC;
            }
            #TelaItem {
                background-color: #F6D8CE;
            }
        </style>
    </head>
    <body>
        <div id="TelaConsulta">
            <ul>
                <li><span class="grid" data-click="editar.php?id=25">Edit</span></li>
                <li><span class="grid" data-click="editar.php?id=35">Edit</span></li>
            </ul>
        </div>
        <div id="TelaEditar">
        </div>
        <div id="TelaItem">
        </div>
    </body>
</html>

Code in editar.php:

<script>
        $(document).ready(function () {
            $(document).on('click', '.item', function (e) {
                if (e.handled !== true) {
                    debugger;
                    var url = 'item.php?id=<?php echo $_GET['id']; ?>';
                    carregaPagina(null, 'TelaEditar', 'TelaItem', url);
                    e.handled = true;
                }
                return false;
            });
            $('#TelaEditar .btnVoltar').click(function () {
                $('#TelaEditar').slideToggle(1000, function () {
                    $('#TelaConsulta').slideToggle(1000);
                    $('#TelaEditar').empty();
                    $('#TelaEditar').removeAttr('style');
                });
                return false;
            });
        });
    </script>
    Id: <?php
      echo $_GET['id'];
    ?><br><br>
    <span class="item">Item</span><br><br>
    <span class="btnVoltar">Voltar</span>

Code in item.php:

<script>
    $(document).ready(function () {
        $('#TelaItem .btnVoltar').click(function () {
            $('#TelaItem').slideToggle(1000, function () {
                $('#TelaEditar').slideToggle(1000);
                $('#TelaItem').empty();
                $('#TelaItem').removeAttr('style');
            });
            return false;
        });
        debugger;
    });
</script>
Id: <?php echo $_GET['id']; ?><br><br>
<span class="btnVoltar">Voltar</span>

In the index.php file click on the 1st Edit.

Insert a debugger in Item button on editar.php note that when you click the button it opens the VM108 and return Id 25.

http://i.stack.imgur.com/RjWER.png

Insert a debugger in the load file item.php note that it opens the VM119 and return Id 25.

http://i.stack.imgur.com/qsljX.png

Soon after I click the back button on item.php file, and then the back button of editar.php. In the index.php file click on the 2nd Edit and return Id 35.

When you click the Item button on editar.php note that it reopens the VM108.

http://i.stack.imgur.com/hSQfj.png

When it appears the item.php file note that it opens the VM141 and return Id 25.

http://i.stack.imgur.com/2rbwf.png

This is my doubt, every time I open the editar.php it always returns me the VM108. When this occurs it is not being updated the item Id and generating the problem.

Link simulation: link

  • 写回答

1条回答 默认 最新

  • weixin_33693070 2016-04-15 12:32
    关注

    The problem is occurring because the click event is still active.

    How am carrying through Ajax HTML and JS code did not know to create a click event it would be active on the screen that I'm carrying the call using $(document).on(), to return to the home screen'm not removing the event you should use the calls $(document).off().

    My editar.php file looked like this:

    <script>
        $(document).ready(function () {
            $(document).on('click', '.item', function (e) {
                if (e.handled !== true) {
                    debugger;
                    var url = 'item.php?id=<?php echo $_GET['id']; ?>';
                    carregaPagina(null, 'TelaEditar', 'TelaItem', url);
                    e.handled = true;
                }
                return false;
            });
            $('#TelaEditar .btnVoltar').click(function () {
                $('#TelaEditar').slideToggle(1000, function () {
                    //Remove click event
                    $(document).off('click', '.item');
                    $('#TelaConsulta').slideToggle(1000);
                    $('#TelaEditar').empty();
                    $('#TelaEditar').removeAttr('style');
                });
                return false;
            });
        });
    </script>
    Código: <?php
        echo $_GET['id'];
    ?><br><br>
    <span class="item">Item</span><br><br>
    <span class="btnVoltar">Voltar</span>
    

    This is because I am passing the Id directly in JS, if perchance I seek the value in a form field for example, there would be no need to clean the event.

    评论

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。