简体   繁体   English

我的Ajax不断刷新整个页面而不是div

[英]my ajax keeps refreshing the whole page instead of the div

I have an ajax form and I want to reload the div instead of the whole page, but it isn't working. 我有一个ajax表单,我想重新加载div而不是整个页面,但是它不起作用。

This is my handling.js: 这是我的handling.js:

$(document).ready(function(){
    $('#guideForm').submit(function(){
        $.ajax({
            type: 'POST',
            url: '../includes/handling.php',
            data: $(this).serialize()
        })
        .done(function(data){
            $('#guide').load(document.URL + ' #guide');
        })
        return false;   
    }); 
});

this is my handling.php: 这是我的handling.php:

if ($_POST['guide']) {
    $settings->addGuide($_POST['guide']); 
}

EDIT: Formcodes: 编辑:表单代码:

<div class="col-md-4">
  <h2>test title</h2>
  <p class="guids">This is a test text.</p>
  <form method="post" id="guideForm">
     <input name="guide" value="1" style="positon:absolute; display:none;">
     <button class="btn btn-primary">Got it!</button>       
  </form>         
</div>

Can someone help me to figure out what I'm doing wrong? 有人可以帮我弄清楚我在做什么错吗?

You should try e.preventDefault() 您应该尝试e.preventDefault()

$('#guideForm').submit(function(e){
        e.preventDefault(); // added this line and an argument 'e' to the function
        $.ajax({
            type: 'POST',
            url: '../includes/handling.php',
            data: $(this).serialize()
        })
        .done(function(data){
            $('#guide').load(document.URL + ' #guide');
        })
        return false;   
    }); 

To stop the page from submitting you need to prevent the default behavior of the event ie to submit the form. 要阻止页面提交,您需要防止事件的默认行为,即提交表单。

Your page keeps reloading because after your request DOM submits the form. 您的页面不断重新加载,因为在您的请求DOM提交表单之后。 And, as default behavior, Your form submits itself on the same page as you haven't specified a action param. 而且,作为默认行为,您的表单会在未指定操作参数的情况下在同一页面上提交。

So, there are multiple ways of doing this. 因此,有多种方法可以做到这一点。

i). 一世)。 Stop the JS event from propagating by using the method preventDefault() of event object. 通过使用事件对象的preventDefault()方法,阻止JS事件传播。

Like this: 像这样:

$(document).ready(function(){
    $('#guideForm').submit(function(e){
        e.preventDefault(); //stop default behaviour
        $.ajax({
            type: 'POST',
            url: '../includes/handling.php',
            data: $(this).serialize()
        })
        .done(function(data){
            $('#guide').load(document.URL + ' #guide');
        })
        return false;   
    }); 
});

This is the most official way of doing this. 这是最官方的方式。

ii). ii)。 You can change your submit button/input to a simple button and bind the ajax with onClick event. 您可以将提交按钮/输入更改为简单按钮,并将ajax与onClick事件绑定。

Like this: 像这样:

$(document).ready(function(){
$('#fakeSubmitButton').click(function(e){
    $.ajax({
        type: 'POST',
        url: '../includes/handling.php',
        data: $(this).serialize()
    })
    .done(function(data){
        $('#guide').load(document.URL + ' #guide');
    })
    return false;   
}); 
});

iii). iii)。 You can define javascript:void(0) as your form's action attribute. 您可以将javascript:void(0)定义为表单的action属性。 This will automatically prevent form from submitting. 这将自动阻止表单提交。

iv). iv)。 By returning false from the event handler. 通过从事件处理程序返回false Which you are currently doing. 您目前正在做什么。 It is strange it is not working as jQuery doc itself says: 奇怪的是,它不能像jQuery文档本身所说的那样工作:

we can cancel the submit action by calling .preventDefault() on the event object or by returning false from our handler. 我们可以通过在事件对象上调用.preventDefault()或从处理程序中返回false来取消提交操作。

Maybe the following post can help you: jQuery - Form still submits with return false 也许以下帖子可以为您提供帮助: jQuery-表单仍提交,但返回false

Also check the answer of Chen-Tsu Lin in the above post. 还要检查以上帖子中林震子的答案。 It is given a link for when you should not use return false . 当您不应该使用return false时,为它提供了一个链接。

Hope this helps. 希望这可以帮助。

Use e.preventDefault(); 使用e.preventDefault();

$(document).ready(function(){
    $('#guideForm').submit(function(e){
        e.preventDefault();
        $.ajax({
            type: 'POST',
            url: '../includes/handling.php',
            data: $(this).serialize()
        })
        .done(function(data){
            $('#guide').load(document.URL + ' #guide');
        })
        return false;   
    }); 
});
$('#guideForm').submit() 

The above jquery statement would by default submit the fold which would essentially reload the code. 上面的jquery语句默认会提交折叠,这实际上将重新加载代码。 If you do not want this to happen, 如果您不希望这种情况发生,

add the following 添加以下内容

event.preventDefault();

This would prevent the submit of the form and execute the code in the function 这将阻止提交表单并执行函数中的代码

I think there's an error in your js that's why it doesn't execute as expected, have you checked if you get any error? 我认为您的js中有一个错误,这就是为什么它未按预期执行的原因,您是否检查了任何错误? Maybe it's because you don't have a termination on your $.ajax function.. try adding ";" 可能是因为您的$ .ajax函数没有终结符。尝试添加“;” terminator. 终结者。

$(document).ready(function(){
    $('#guideForm').submit(function(){
        $.ajax({
            type: 'POST',
            url: '../includes/handling.php',
            data: $(this).serialize()
        })
        .done(function(data){
            $('#guide').load(document.URL + ' #guide');
        });

        return false;   
    }); 
});

Use e.preventDefault() ; 使用e.preventDefault() ; to stop the default behavior of submit. 停止提交的默认行为。

$(document).ready(function(){
    $('#guideForm').submit(function(e){
       e.preventDefault(); //stop default behaviour
       $.ajax({
           type: 'POST',
            url: '../includes/handling.php',
            data: $(this).serialize()
        })
        .done(function(data){
            $('#guide').load(document.URL + ' #guide');
        })
        return false;   
    }); 
});

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

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