在AJAX调用后防止jQuery自动重定向

时间:2022-12-01 22:25:31

I have an ajax call that loads a div into a popup, but when I click on button, the trigger happens, the div gets loaded then the entire page gets reloaded. I just wanted to know how could I find this second function that is reloading my entire page and add the exception, so that on me adding the div it does not trigger? I have tried return false and event.preventDefault(); but that didn't work

我有一个ajax调用,它将一个div加载到一个弹出窗口中,但是当我点击按钮时,触发器就会发生,div被加载,然后整个页面被重新加载。我只是想知道如何找到第二个函数重载整个页面并添加异常,这样我添加div就不会触发了?我尝试返回false和event.preventDefault();但这并不工作

My AJAX call code is like this

我的AJAX调用代码是这样的

        $('.trigger').on('click', function(){
          $.ajax({
            url: '../load_div.php',
            type: 'POST',
            data: {
              data: data
            },
            datatype: 'JSON'
          }).done( function(data) {
              console.log(data);
              $('.div_class').html(data);
            });

          });

1 个解决方案

#1


3  

A submit button will automatically load another page. to avoid this. You can use

提交按钮将自动加载另一个页面。为了避免这种情况。您可以使用

<button type="button"></button>

or

<input type="button">

or in your event handler

或者在事件处理程序中

$('.trigger').on('click', function(event){
   event.preventDefault(); //prevents page redirect
   $.ajax({
     url: '../load_div.php',
     type: 'POST',
     data: {
       data: data
     },
     datatype: 'JSON'
   }).done( function(data) {
       console.log(data);
       $('.div_class').html(data);
   });
});

#1


3  

A submit button will automatically load another page. to avoid this. You can use

提交按钮将自动加载另一个页面。为了避免这种情况。您可以使用

<button type="button"></button>

or

<input type="button">

or in your event handler

或者在事件处理程序中

$('.trigger').on('click', function(event){
   event.preventDefault(); //prevents page redirect
   $.ajax({
     url: '../load_div.php',
     type: 'POST',
     data: {
       data: data
     },
     datatype: 'JSON'
   }).done( function(data) {
       console.log(data);
       $('.div_class').html(data);
   });
});