2015-10-09 15 views
0

我試圖在放置在phtml模板中的一個按鈕中自動運行onclick函數。如何在按鈕中自動運行Onlick

這與按鈕的代碼的HTML文件:

<button type="button" id="review-btn" title="<?php echo $this->__('Place Order') ?>" class="button btn-checkout" onclick="review.save();"><span><span><?php echo $this->__('Place Orderxxxxx') ?></span></span></button> 

這是保存和審查功能的JavaScript文件的一部分:

//review function starts 
var Review = Class.create(); 
Review.prototype = { 
    initialize: function(form,saveUrl,successUrl,agreementsForm){ 
     this.form = form; 
     this.saveUrl = saveUrl; 
     this.successUrl = successUrl; 

     this.agreementsForm = agreementsForm; 
     this.onSave = this.nextStep.bindAsEventListener(this); 
     this.onComplete = this.resetLoadWaiting.bindAsEventListener(this); 
    }, 
     //function triggers when onloading on review save function 
    loadingbox: function() { 
     var translate = Translator.translate('processing').stripTags(); 
     $("review-please").update(' <div class="please-wait-loading">&nbsp;</div><span class="load-wait">'+translate+'</span>') 
    var form = $('review-btn'); 
    form.disabled='true'; 

    }, 

    save: function(){ 
var paymentmethod = payment.currentMethod; 
     var validator = new Validation(this.form); 
     if (validator.validate()) { 


      var request = new Ajax.Request(
       this.saveUrl, 
       { 
        method:'post', 
        parameters: Form.serialize(this.form), 
        onLoading:this.loadingbox.bind(this), 
        onComplete: this.onComplete, 
        onSuccess: function(transport) { 
         if(transport.status == 200) { 
          var data = transport.responseText.evalJSON(); 
          if(!data.success) 
          { 
           alert(data.error_messages); 
           $("review-please").update(''); 
           $('review-btn').disabled=''; 

          } 
          if (data.redirect) { 
           location.href = data.redirect; 
           return; 
          } 
          if(data.success){ 
           //hostedpro and advanced payment action 
           if(paymentmethod == 'hosted_pro' || paymentmethod =='payflow_advanced') 
           { 
             Element.hide('review-please'); 
             Element.hide('review-btn'); 
             document.getElementById('checkout-paypaliframe-load').style.display= 'block'; 
             iframedata = data.update_section["html"].replace("display:none","display:block"); 
             document.getElementById('checkout-paypaliframe-load').innerHTML = iframedata; 

           } 
           else //other payment action 
           { 
            this.isSuccess = true; 
            window.location = data.success; 
           } 
          } 
         } 
        }, 
       onFailure: checkout.ajaxFailure.bind(checkout) 
      } 
      ); 
    //var updater = new Ajax.Updater('product-details', this.saveUrl, {method: 'post',parameters: Form.serialize(this.form)}); 
    } 
}, 

如果我只是改變的onclick到的setTimeout它doesn沒有工作。

非常感謝您

+1

改爲在調用onClick時可以直接使用setTimeout(review.save(),10000);使用它它會在一段時間後調用函數 更多參考http://stackoverflow.com/a/11901175/4944490 –

回答

3

使用setTimeout在你的JavaScript文件。

第二個參數是以毫秒(1000ms = 1s)爲單位的時間,之後將執行該函數。

setTimeout(review.save, 1000); 

編輯:

Sinde你在函數中使用this,你需要重寫this。如果獨立調用,則範圍不再相同。

setTimeout(function(){ 
    review.save.apply(document.getElementById('review-btn')); 
}, 1000); 

的完整代碼

添加到您的JS文件的最後一行。

window.onload = function(){ 
    setTimeout(function(){ 
     review.save.apply(document.getElementById('review-btn')); 
    }, 1000); 
}; 
+0

謝謝,但它不起作用。它不會保存任何東西。 –

+0

但它的調用功能,對吧?如果你不顯示更多的代碼,我無法幫助你。保存是另一個話題。 –

+0

我不知道調用函數是否工作,因爲頁面上沒有改變。頁面在這個按鈕上面使用ajax,也許這就是原因。 –

相關問題