javascript - jQuery 表单超时不提交 - e[h] 不是函数

标签 javascript jquery html forms

这个问题之前在SO上被问过但没有成功,也许是不同的场景

这是我的表单

<form id="testSubmit" method="post" action="submit.php">
     <!--The value of following field will be obtained when user submits test or time runs out -->
     <input type="hidden" name="user_answer" id="user_answer" value=""/>
     <input type="submit" id="submit" style="padding:5px;" value="Submit"/>
</form>

这是我的 Javascript 和 jQuery 代码

<script type="text/javascript">
    $('document').ready(function()
    {
   //pick up the options selected by the student and populate #userAnswer
        $('#testSubmit').submit(function()
        {
            var string='';
            $('.question').each(function () 
            {
                string += $(this).find('input[type="radio"]:checked').length ? $(this).find('input:checked').val() : 'z';
            });
            $('#user_answer').val(string);
        });

//timer for countdown and to submit the form
        var delay=5;
        function countdown()
        {
            setTimeout(countdown,1000);
            $('#timer').html("Time"+delay);
            delay--;
            if(delay<0)
            {
                $("#testSubmit").submit();
                delay=0;

            }
        }
    countdown();
    });
    </script>

但这不会提交并给出错误 e[h] is not a function ;

我也试过document.getElementById("testSubmit").submit()并给出错误 document.getElementById("testSubmit").submit() is not a function .

document.forms[0].submit 相同的错误

编辑

我放了一个 alert('Hello');在定时器内部并且它工作。所以定时器工作正常。问题是一旦时间达到零,就会出现错误。

最佳答案

“但这不会提交并给出错误 e[h] 不是函数;”

将 id="submit"更改为其他内容。原因是,如果您有一个 ID 为 submit 的输入元素,它将使用 HTMLInputElement 为表单设置一个 submit 属性。因此,您无法在 HTMLFormElement 上调用 .submit 方法。

关于javascript - jQuery 表单超时不提交 - e[h] 不是函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21577902/

相关文章:

javascript - 我可以将 jstree 复选框限制为只有有 child 的 parent 吗?

javascript - 如何使移动设备中的 jquery setinterval 移动更顺畅?

jquery - 简单的滚动到动画闪烁

html - 如何在 float 元素中创建可滚动元素?

JQuery 通过 div ID 添加类/删除类

Javascript promise 不等待解决

javascript - Node.js 连接到 mongodb

javascript - getElementsByTagName(...).0.parentNode' 为 null 或不是对象

javascript - 如何在不改变源的情况下返回结果?

javascript - Mobile Safari、jQuery 和绑定(bind)到 future 的 DOM 元素