Javascript:延迟提交无法正常工作

时间:2016-03-02 13:39:41

标签: javascript submit delay

https://jsfiddle.net/z1ctm0hd/

JSFiddle生成一个奇怪的错误列表(图片http://i.share.pho.to/7e8051a3_l.png),但是,自动提交正在我的页面上工作,但没有请求延迟。任何人都可以寻求帮助。

<form id='formBlokUziv' action='' method='post'>

  <div class='onoffswitch'>
        <input type='checkbox' name='onoffswitch1' value='1'   onchange='setTimeout(document.getElementById("formBlokUziv").submit(), 5000);' class='onoffswitch-checkbox' id='myonoffswitch1' >
    <label class='onoffswitch-label' for='myonoffswitch1'>
        <span class='onoffswitch-inner'></span>
        <span class='onoffswitch-switch'></span>
    </label>
  </div>

  <div class='onoffswitch'>
        <input type='checkbox' name='onoffswitch2' value='2' onchange='setTimeout(document.getElementById("formBlokUziv").submit(), 5000);' class='onoffswitch-checkbox' id='myonoffswitch2' >
    <label class='onoffswitch-label' for='myonoffswitch2'>
        <span class='onoffswitch-inner'></span>
        <span class='onoffswitch-switch'></span>
    </label>
  </div>

  <div class='onoffswitch'>
        <input type='checkbox' name='onoffswitch3' value='3' onchange='setTimeout(document.getElementById("formBlokUziv").submit(), 5000);' class='onoffswitch-checkbox' id='myonoffswitch3' >
    <label class='onoffswitch-label' for='myonoffswitch3'>
        <span class='onoffswitch-inner'></span>
        <span class='onoffswitch-switch'></span>
    </label>
  </div>

</form>

1 个答案:

答案 0 :(得分:2)

作为setTimeout调用中的第一个参数输入的命令将立即执行。将每个超时放入一个函数中,它将在指定的超时后运行。像 -

这样的东西
setTimeout(function() { document.getElementById("formBlokUziv").submit() }, 5000);