简体   繁体   English

.ajax()在按ENTER键后刷新页面

[英].ajax() refreshes the page after ENTER is hit

I am using ajax to update the db with a new folder but it refreshes the page after ENTER is hit. 我正在使用ajax用新文件夹更新数据库,但在按ENTER键后将刷新页面。 on my form I have onkeypress="if(event.keyCode==13) savefolder();" 在我的表单上,我有onkeypress="if(event.keyCode==13) savefolder();" here is the javascript code that I have: what it does basically is after you hit enter it calls the function savefolder, savefolder then sends a request through ajax to add the folder to the db. 这是我拥有的javascript代码:基本上,它的作用是在您按Enter键后调用函数savefolder,savefolder然后通过ajax发送请求以将该文件夹添加到db。 Issue is it refreshes the page... I want it to stay on the same page. 问题是它刷新了页面...我希望它保留在同一页面上。 any suggestions? 有什么建议么? Thank you 谢谢

<script>


 function savefolder() {

var foldername= jQuery('#foldername').val(),
    foldercolor= jQuery('#foldercolor').val();
//  ajax request to add the folder
            jQuery.ajax({   
                type: 'get',
                url: 'addfolder.php',
                data: 'foldername=' + foldername + '&foldercolor=' + foldercolor,       
                beforeSend: function() { alert('beforesend');}, 
                success: function() {alert('success');}
            });

return false;
}
</script>

This is working: 这正在工作:

<form>
    <input type="submit" value="Enter">
    <input type="text" value="" placeholder="search">
</form>

function savefolder() {
    var foldername= jQuery('#foldername').val(),
        foldercolor= jQuery('#foldercolor').val();

    jQuery.ajax({   
        type: 'get',
        url: '/echo/html/',
        //data: 'ajax=1&delete=' + koo,       
        beforeSend: function() {
            //fe('#r'+koo).slideToggle("slow");
        },
        success: function() {
            $('form').append('<p>Append after success.</p>');
        }
    });

    return false;
}

jQuery(document).ready(function() {
    $('form').submit(savefolder);
});

http://jsfiddle.net/TFRA8/ http://jsfiddle.net/TFRA8/

You need to check to see if you're having any errors during processing (Firebug or Chrome Console can help). 您需要检查在处理过程中是否有任何错误(Firebug或Chrome Console可以帮助您)。 As it stands, your code is not well-formed, as the $(document).ready() is never closed in the code you included in the question. 就目前而言,您的代码格式不正确,因为$(document).ready()从未在问题中包含的代码中关闭。

Simply stop the propagation of the event at the time of the form submission 只需在提交表单时停止事件的传播

jQuery(document).ready(function($) {
  $("#whatever-form-you-are-pulling-your-values-from").submit(function(event) {
    var foldername = $('#foldername').val();
    var foldercolor = $('#foldercolor').val();

    event.stopPropagation();

    //  ajax request to add the folder
    $.ajax({
      type: 'get',
      url: '../addfolder.php',
      data: 'ajax=1&delete=' + koo,       
      beforeSend: function() { fe('#r'+koo).slideToggle("slow"); }, 
      success: function() {  }
  });
});

Since by default on a form the enter button submits the form, you need to not only handle this with your own code, but cancel the event after. 由于默认情况下,在表单上输入按钮会提交表单,因此,您不仅需要使用自己的代码来处理此问题,还需要在之后取消该事件。

Try this code instead: 请尝试以下代码:

onkeypress="if(event.keyCode==13) {savefolder(); return false;}"

The onkeypress event will that the return value of the javascript and only continue with it's events if it returns true. onkeypress事件将指示javascript的返回值,并且仅在返回true时才继续其事件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM