jQuery表单验证 - 未捕获TypeError:undefined不是函数

时间:2014-10-21 09:44:48

标签: jquery jquery-validate

我正在使用http://jqueryvalidation.org/来验证表单。我得到的错误是Uncaught TypeError: undefined is not a function错误来自下面的JS代码中的一行

JS

submitHandler如下

submitHandler: function (form) {
  error2.hide();
  form[0].submit(); // error happens on this line
}

HTML

<form action="login.asp" method="post" id="form_sample_2" class="form-horizontal">
    <div class="form-body">
        <div class="alert alert-danger display-hide">
            <button class="close" data-close="alert"></button>
            You have some form errors. Please check below.
        </div>

        <div class="form-group">
            <label class="control-label col-md-3">Username <span class="required">
                                            * </span>
            </label>
            <div class="col-md-6">
                <div class="input-icon right">
                    <i class="fa"></i>
                    <input type="text" class="form-control" name="username" />
                </div>
            </div>
        </div>
        <div class="form-group">
            <label class="control-label col-md-3">Password <span class="required">
                                            * </span>
            </label>
            <div class="col-md-6">
                <div class="input-icon right">
                    <i class="fa"></i>
                    <input type="password" class="form-control" name="password" />
                </div>
            </div>
        </div>
        <div class="form-group">
            <label class="control-label col-md-3">PIN <span class="required">
                                            * </span>
            </label>
            <div class="col-md-6">
                <div class="input-icon right">
                    <i class="fa"></i>
                    <input type="text" class="form-control" name="pin" />
                </div>
            </div>
        </div>

    </div>
</form>

表单实际提交并且错误显示需要的地方但是这个JS不断提交表单。有什么建议吗?

2 个答案:

答案 0 :(得分:2)

form paramsin,submitHandler是form dom元素,因此form[0]将是未定义的,这会导致问题。

submitHandler: function (form) {
  error2.hide();
  form.submit(); // error happens on this line
}

答案 1 :(得分:0)

试试这个:

submitHandler: function (form) {
     $( '#error2' ).hide();
     form.submit(); 
}