Html.BeginForm使用的onsubmit验证 [英] Html.BeginForm using onSubmit to validate

查看:1687
本文介绍了Html.BeginForm使用的onsubmit验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图提交我的形式,但我已经添加了一些jQuery的验证,因而作品:

Am trying to submit a my form , however i have added some jquery for validation which works:

    $(function validateForm() {
    $("#newMonoReading").on('focusout', function () {
        var str = "";

        var initialMonoReading = $('#InitialMonoReading').val();
        var newMonoReading = $('#newMonoReading').val()
        if (~~newMonoReading < ~~initialMonoReading) {
            $('#MonoErrorMessage').text("New Mono Readings must be MORE than existing");
            $('#MonoErrorMessage').show();
            return false;


        } else {
            $('#MonoErrorMessage').hide();

        }
    });
});

但问题是提交按钮

But the problem is on the submit button

  <input type="submit" value="Submit" class="btn btn-primary">

如果有错误的提交按钮仍然有效,我需要它不显示错误信息,然后发送,但我尝试使用的onsubmit的形式,但其没有工作,但仍提出,即使该值小于和该错误信息显示。

If there is an error the submit button still works, i need it to show no error messages and then send, but i tried using the onsubmit for the form but its not working, it still submits even though the value is less and the error message is showing.

这是html.BeginForm。我认为该错误可能是在这里。我真的不知道。

This is the html.BeginForm. i think the error may be here. am not really sure.

         <div class="modal-body">
        @using (Html.BeginForm("Save", "ReadingsEntry", FormMethod.Post, new { enctype = "multipart/form-data", onsubmit = "validateForm()" }))
        {

任何想法

推荐答案

如果我是正确的,然后提交您希望与您的jQuery的功能验证您的形式..而如果vailidation传递,那么你要提交被contineued否则你要提交被取消。如果我错了,那么请clerify你的问题多一点。

If I am correct, then on submit you want to validate your form with your jquery function.. and If vailidation is passed then you want the submit to be contineued else you want the submit to be canceled.. If I am wrong then please clerify your question a bit more.

否则,你可以改变你的jQuery函数 -

Else you can change your jquery function to --

$(function validateForm(event) {
    event = event || window.event || event.srcElement;
    var initialMonoReading = $('#InitialMonoReading').val();
    var newMonoReading = $('#newMonoReading').val()
    if (~~newMonoReading < ~~initialMonoReading) {
        $('#MonoErrorMessage').text("New Mono Readings must be MORE than existing");
        $('#MonoErrorMessage').show();
        event.preventDefault();
    }
    else {
        $('#MonoErrorMessage').hide();
    }
});

和而创建标记,使用以下行 -

and while creating markup, use following line-

@using (Html.BeginForm("Save", "ReadingsEntry", FormMethod.Post, new { enctype = "multipart/form-data", onsubmit = "validateForm(event)" }))

虽然,我没有检查你code,但这应该工作。请尝试一下,让我们知道。

Although, I didn't checked you code, but this should work.. Please try it and let us know..

这篇关于Html.BeginForm使用的onsubmit验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆