如何重新运行javascript代码

时间:2014-02-24 11:46:19

标签: javascript

我有一个脚本和他的代码片段:

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 === ''为真,如何重新运行整个脚本?

3 个答案:

答案 0 :(得分:1)

如果你想再次重新运行你的事件,你可以尝试使用这样的东西,在这种情况下你会得到一个递归。(所以你应该在条件下修复它)

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();
        }
    };
}

答案 1 :(得分:0)

有没有听说过命名函数?

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();
      }
   };    
}

答案 2 :(得分:0)

试试这样。

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 */
        }
    };
};