2016-12-14 35 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按鈕在屏幕上提交和顯示數組(這是不起作用的)。我必須使用相同的按鈕來顯示另一個數組,但我無法觸摸他的代碼並將其添加到此功能。因爲它是不同的東西。我必須一起使用提交和按鈕類型。你明白了 ?或者你想要所有的頁面代碼。 –