duanlang1196 2014-11-07 20:26
浏览 93
已采纳

为什么从AJAX返回的值几乎立即消失?

this is my server-side code:

modify_emp.php

<?php
    echo $_POST[id];
?>

And this is my Javascript in my html page:

<script>

  $(document).ready(function () {

  var alreadyClicked = false;

  $('.element').hover( 
    //mouseenter function
    function(){
        $('.element').click(
            function(){
            $(this).css("color","blue");
            var objName = $(this).attr('name');
            var objColumn = $(this).attr('id');
            if(!alreadyClicked){
                alreadyClicked = true;
                $(this)
                .prepend('<form method="POST" class="newInput"><input type="text" name="newInput" style="width:140px"></input></form>');
                var elemento = $(".newInput");
                var position = elemento.position();
                $(".newInput").css({
                    'position': 'absolute',
                    'top': position.top + 15,
                    'opacity':0.9,
                    'z-index':5000,
                })
                .focus();
                //on enter keypress
                $(document).keypress(function(e) {
                    if(e.which == 13) {

                        $.ajax({

                            url: 'modify_imp.php',
                            type: 'post',
                            data: { id : objName, column : objColumn },
                            success: function(data, status){
                                $("#debug").html(data);
                            }

                        });
                    }
                });
             } //if (!alreadyClicked) end
        }); //end mouseenter function
   },
   //mouseleave function
   function () {
    alreadyClicked = false;
    $(this).css("color","red");
    $(".newInput").remove();
   }
 ); //end .hover

});

The debug is a <div id="debug"> </div> at the end of my html page where i want to show my response from server. When i press 'ENTER' I can actually see the value for 0.1s inside that div, but then it disappears.

I already tried to pass the return value to a local or global variable but it didn't work. For some reason the value inside response is lost after 0.1s, even if i pass it to another variable elsewhere.

Can someone explain me why and how can i "store" the server response?

EDIT: Just edited with my entire <script>

  • 写回答

3条回答 默认 最新

  • donglu5728 2014-11-07 20:33
    关注

    Since you see the result momentarily, I'm going to hazard a guess that you have a form element on your page and when you hit return, it's actually submitting the form. You briefly see the result of the ajax operation and then your form submits causing the page to reload as a new blank page. This is a common issue and always has these same symptoms.

    You can either remove the form element or block the default submission of the form with javascript.

    If you show us more of your actual HTML, we could help more specifically with how to prevent the form from submitting.

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

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!