duanmao2650 2013-03-14 19:31
浏览 55
已采纳

如何在没有错误后捕获表单提交?

I have a form that is checking for valid fields after a user clicks submit. It looks like this:

<form class="form-horizontal" method="post" action="#" name="basic_validate" id="basic_validate" />             
    <div class="control-group">
        <label class="control-label">Email Recipients</label>
        <div class="controls">
            <input type="text" name="emailreceipients" id="emailreceipients" />
        </div>
    </div>
    <div class="form-actions">
        <input type=submit value="Send" id="sendbtn" class="btn btn-primary" />
    </div>
</form>

If an error is seen a class called: 'error' is added to the field.

How can I capture the form submission after all errors have been removed (e.g. user enters all data)

Here is the validation call that gets hit after form submission:

// Form Validation
$("#basic_validate").validate({
    rules:{
        required:{
            required:true
        },
        emailreceipients:{
            required:false
        }
    },
    errorClass: "help-inline",
    errorElement: "span",
    highlight:function(element, errorClass, validClass) {
        $(element).parents('.control-group').addClass('error');
    },
    unhighlight: function(element, errorClass, validClass) {
        $(element).parents('.control-group').removeClass('error');
        $(element).parents('.control-group').addClass('success');
    }
});

I've tried something like this:

$("#sendbtn").click(function (e) {
    e.preventDefault();

    if ($("#basic_validate").children('.control-group').hasClass('error')) {
        alert("Error Seen");
    }
    else {
        // Process Form
    }
});

Any idea how I can accomplish this?

  • 写回答

2条回答 默认 最新

  • dongling2545 2013-03-14 19:38
    关注

    Use jQuery's submit()

    $("#basic_validate").submit();
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 用verilog实现tanh函数和softplus函数
  • ¥15 求京东批量付款能替代天诚
  • ¥15 slaris 系统断电后,重新开机后一直自动重启
  • ¥15 51寻迹小车定点寻迹
  • ¥15 谁能帮我看看这拒稿理由啥意思啊阿啊
  • ¥15 关于vue2中methods使用call修改this指向的问题
  • ¥15 idea自动补全键位冲突
  • ¥15 请教一下写代码,代码好难
  • ¥15 iis10中如何阻止别人网站重定向到我的网站
  • ¥15 滑块验证码移动速度不一致问题