2016-04-20 19 views
0

私は2つのコールバックを持っています。そのうちの1つはブール値を返し、もう1つはajax呼び出しを行う必要があります。 2番目の結果から結果を得ることはできません。コールバック関数から返されるajaxの結果

私はreturn the response from an asynchronous callの方法の説明を読みましたが、結果は得られません。

私のコードがあります:

if($.fn.wizard) { 
     $('#wzd-enrollment').wizard({ 
      //some code 

      onStepLeave: function (wizard, step){ 
       //in this function i have always to return a boolean value to move to the next step or not 
       var result; 
       result = doAjax(wizard, step); 
       console.log(result); //always log undefined 
       return result; 
      } 
     }); 

     function doAjax(wizard, step){ 
      if(step.id == 'step-1'){ 
       //some code 
       $.ajax({ 
        type: "GET", 
        dataType: 'json', 
        url: s_url, 
       }).done(function(data) { 
        //some code 
        return true; 

       }).fail(function(jqXHR, textStatus, errorThrown){ 
        //some code 
        return false; 
       }); 
      }else{ 
       return true; 
      } 
     } 
    } 
+0

あなたが天気をかを伝えることができないので、あなたは非同期機能 –

+1

から何かを返すことはできませんそれが呼び出されるとき。またはどのくらい頻繁に。 Promiseを使用すると、jQueryが提供します。 – Thomas

+0

あなたのリプレイに感謝しますが、私は何をすべきですか? –

答えて

0

あなたはdocument.ready上doAjax機能を行う必要があります。 ステップ離脱の際にこれを行う必要はありません。

これが私の最後の答えに合ったコードである

var s_url = ''; // fill out your url here 
// save the result in a var 
var resultStep1 = true; 

if($.fn.wizard) { 
    $('#wzd-enrollment').wizard({ 
     //some code 

     onStepLeave: function (wizard, step){ 
      var result2 = false; 
      return checkResult(step); 
     } 
    }); 
} 

function doAjax(){ 
    //some code 
    $.ajax({ 
     type: "GET", 
     dataType: 'json', 
     url: s_url, 
    }).done(function(data) { 
     //some code 
     resultStep1 = true; 

    }).fail(function(jqXHR, textStatus, errorThrown){ 
     //some code 
     resultStep1 = false; 
    }); 
} 

function checkResult(step) { 
    if(step.id == 'step-1'){ 
     return resultStep1; 
    } 
    else { 
     return true; 
    } 
} 

// get the result on page load 
$(document).ready(function() { 
    doAjax(); 
}); 
+0

トリガーイベントはonStepLeaveでなければならないため、ドキュメント準備ができていません。フォームデータがあり、ajaxフォームで送信する必要があります。 –

+0

ああ、あなたはフォームにボタンを追加できますか?独自の「次へ」ボタンを実装し、ウィザードの「次へ」ボタンを隠すことは素晴らしいことです。次にクリックすると、あなたのajax呼び出しがトリガーされます。返ってきたら、jqueryで実際の '次の'ボタンをクリックします。 –

0

以下のコードをチェックしてください:

<input type="button" value="Next" id="wizard_next_button" style="display: none" /> 
<input type="button" value="Next" id="your_next_button" style="display: block" /> 
<script> 
var s_url = ''; // fill out your url here 
// save the result in a var 
var result = true; 

if($.fn.wizard) { 
    $('#wzd-enrollment').wizard({ 
     //some code 

     onStepLeave: function (wizard, step){ 
      return result; 
     } 
    }); 
} 

function doAjax(callback){ 
    if($("#wizard").steps("getCurrentIndex") == 'step-1'){ 
     //some code 
     $.ajax({ 
      type: "GET", 
      dataType: 'json', 
      url: s_url, 
     }).done(function(data) { 
      //some code 
      if (typeof(callback) == 'function') { 
       callback(true); 
      } 

     }).fail(function(jqXHR, textStatus, errorThrown){ 
      //some code 
      if (typeof(callback) == 'function') { 
       callback(false); 
      } 
     }); 
    } else { 
     if (typeof(callback) == 'function') { 
      callback(true); 
     } 
    } 
} 

// get the result on page load 
$("#your_next_button").click(function() { 
    doAjax(function(retval) { 
     result = retval; 
     $("#wizard_next_button").trigger('click'); 
    }); 
}); 
</script> 
+0

私はこの解決策を試してみます。まだ最高ではありません。ありがとう@Alain –

+0

よろしくお願いいたします。私はそれが最も美しい答えではないことを知っている..しかし、私はそれがうまくいくと思います。 –

関連する問題