当仍有东西要加载时停止表单提交

时间:2014-08-16 18:04:45

标签: javascript forms

我写了一段代码。我只是想看看它是否有效!考虑并行下载。 我正在禁用按钮,然后在处理表单的脚本标记之后启用它。 我想确保用户在加载必要的文件之前不提交表单,例如“checkform.js”

<input type="text"><br>
<input type="submit" value="submit" id="submit">
<script>
    document.getElementById('submit').setAttribute('disabled','disabled');
</script>

<script src="checkform.js"></script>
<script>
    document.getElementById('submit').removeAttribute('disabled');
</script>

2 个答案:

答案 0 :(得分:2)

试试这个: 1.在提交btn的属性中设置禁用。 2.将onload属性添加到checkform脚本标记。

<input type="text"><br>
<input type="submit" value="submit" id="submit" disabled="disabled">

<script>
    function checkformLoaded() {
        document.getElementById('submit').removeAttribute('disabled');
        // do whatever else you want after the script is loaded!
    }
</script>

<script src="checkform.js" onload="checkformLoaded"></script>

答案 1 :(得分:1)

您也可以等待整个window加载。这将在加载所有资源时触发,包括检查表单的JS文件。:

window.onload = function() {
    document.getElementById('submit').removeAttribute('disabled');
}