2016-12-14 59 views
1

我正在一个Web应用程序,我有一个问题,返回的Ajax。如何修复返回Ajax在一个div没有返回false

我可以看到AJAX返回的结果,但我的问题是这个结果出现然后消失。我发现一个返回false的解决方案,问题是,一旦显示完成,我返回false,我的div仍然在页面上,但我不能重复使用我的onclick按钮(调用我的JavaScript函数等ajax调用)。

如果有人有解决方案。

这是代码:

<input class="Admin-Bouton" type="submit" onclick="affOtp();return false" value="Générer" form="formParametres" /> 

<div id="syntheseOTP"></div> 

function affOtp() { 
    $(function(){ 
     $.ajax({ 
      url: 'vue/ressources/affOtp.php', 
      type: 'POST', 
      dataType: 'html', 
      data: { idEmploye: nom }, 
      success: function(msg) { 
       console.log(msg); 
       document.getElementById("syntheseOTP").innerHTML = msg; 
      }, 
      error: function(resu, statut, erreur) { 
       console.log(erreur); 
       alert("papa"); 
      } 
     }); 
    }); 
}; 
+2

删除'文档ready'处理程序即'$从代码(函数(){...})'和'使用类型= “按钮”' – Satpal

+0

嘿Satpal,你的答案是完美的,但我必须保持type =“submit”...我不知道你是否有解决方案 –

+0

把你的ajax调用结束时返回false,而不是内联的按钮属性。否则,您需要为onclick创建一个匿名函数包装并从中返回false。 – codemonkey65

回答

3

这种情况发生,因为形式将所需提交和页面刷新,你能避免通过使用类型button代替submit

<input id="Admin-Bouton" class="Admin-Bouton" type="button" onclick="affOtp();return false" value="Générer" form="formParametres" /> 

我建议附加事件点击js代码而不是内联事件onclick

$('#Admin-Bouton').on('click', affOtp); 

HTML将是这样的:

<input id="Admin-Bouton" class="Admin-Bouton" type="button" value="Générer" form="formParametres" /> 

希望这有助于。

+0

嘿Zakaria感谢你的回复,这是很好的我尝试它,但是我必须保持提交类型...因为这个按钮是用于其他的东西。如果没有办法,我会创建另一个按钮。 –

+0

Okay A. Wolff所以这就像我返回false,但我不能重用按钮 –

+0

@ TomLgl-dcl你必须明确提交。 – Jai

0

编辑:在affOtp()之前遗漏“返回”;

<input class="Admin-Bouton" type="submit" onclick="return affOtp();" value="Générer" form="formParametres" /> 

<div id="syntheseOTP"></div> 

function affOtp() { 
     $(function(){ 
      $.ajax({ 
       url: 'vue/ressources/affOtp.php', 
       type: 'POST', 
       dataType: 'html', 
       data: { idEmploye: nom }, 
       success: function(msg) { 
        console.log(msg); 
        document.getElementById("syntheseOTP").innerHTML = msg; 
       }, 
       error: function(resu, statut, erreur) { 
        console.log(erreur); 
        alert("papa"); 
       } 
      }); 
    }); 
    return false; 
}; 
+0

表单将提交.....! – Jai

+0

如果在提交的onclick上调用的函数返回false,则不应提交。请尝试一下。 – codemonkey65

+1

onclick =“return affOtp();” – degr

0

由于没有一个答案给你还在工作,我会建议你做如下更改(记住,你不想改变type="submit")。

<input id="AjaxButton" class="Admin-Bouton" type="submit" value="Générer" form="formParametres" /> 
<!-- remove onlick attribute and add a Id attribute in above line --> 

<div id="syntheseOTP"></div> 

现在确保您在脚本中添加了单击事件处理程序。

$('#AjaxButton').on('click',affOtp); 

function affOtp(e) { // add e as input parameter 
         // removed unnecessary wrapper 
    e.preventDefault(); // this will now stop the click event. 

    $("#syntheseOTP").html("Loading...."); // add loading text into div 
     $.ajax({ 
      url: 'vue/ressources/affOtp.php', 
      type: 'POST', 
      dataType: 'html', 
      data: { idEmploye: nom }, 
      success: function(msg) { 
       console.log(msg); 
       $("#syntheseOTP").html(msg); //refactored to use jquery syntax 
      }, 
      error: function(resu, statut, erreur) { 
       console.log(erreur); 
       alert("papa"); 
      } 
     }); 
}; 
+0

结果很好地显示了affOtp函数的结果,但第二个函数不起作用..我想我会找到另一个解决方案或创建其他按钮,即使那是不想要我的老板!感谢传递时间来帮助我Rajshekar。 –

+0

@ TomLgl-dcl'第二个功能不行'你能详细说明一下吗? –

+0

如果我尽可能轻松地解释这一点。在我之前的另一位程序员使用Générer按钮在屏幕上提交和显示数组(这是不起作用的)。我必须使用相同的按钮来显示另一个数组,但我无法触摸他的代码并将其添加到此功能。因为它是不同的东西。我必须一起使用提交和按钮类型。你明白了 ?或者你想要所有的页面代码。 –