簡體   English   中英

如何重新運行JavaScript代碼

[英]How to re-run javascript code

我有一個腳本和他的代碼片段:

but.onclick = function() {
    var form;
    var constructor = new Constructor();
    var builder = new BidFormBuilder();
    constructor.setFormBuilder(builder);
    constructor.constructForm();
    constructor.displayForm();
    form = constructor.getForm();
    form.getSubmitButton().onclick = function cl() {
        var phone = form.getPhone().childNodes[1].childNodes[1];
        if (phone.value === '') {
            //Re-run code instruction must been here.
        }
    };
};

phone.value === ''為true時,如何重新運行整個腳本?

如果您想再次運行事件,則可以嘗試使用類似的方法,在這種情況下,您將獲得遞歸。

but.onclick = function() {
    execute();
};


function execute(){
    var form;
    var constructor = new Constructor();
    var builder = new BidFormBuilder();
    constructor.setFormBuilder(builder);
    constructor.constructForm();
    constructor.displayForm();
    form = constructor.getForm();
    form.getSubmitButton().onclick = function cl() {
        var phone = form.getPhone().childNodes[1].childNodes[1];
        if (phone.value === '') {
            //Re-run code instruction must been here.
            execute();
        }
    };
}

聽說過命名函數嗎?

but.onclick = yourfunctionhere; 

function yourfunctionhere() {
   var form;
   var constructor = new Constructor();
   var builder = new BidFormBuilder();
   constructor.setFormBuilder(builder);
   constructor.constructForm();
   constructor.displayForm();
   form = constructor.getForm();
   form.getSubmitButton().onclick = function cl() {
      var phone = form.getPhone().childNodes[1].childNodes[1];
      if (phone.value === '') {
         yourfunctionhere();
      }
   };    
}

這樣嘗試。

but.onclick = clickHandler;

function clickHandler() {
    var form;
    var constructor = new Constructor();
    var builder = new BidFormBuilder();
    constructor.setFormBuilder(builder);
    constructor.constructForm();
    constructor.displayForm();
    form = constructor.getForm();
    form.getSubmitButton().onclick = function cl() {
        var phone = form.getPhone().childNodes[1].childNodes[1];
        if (phone.value === '') {
            clickHandler(); /* Call the function again */
        }
    };
};

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM