Submit MVC3 form that uses Data Annotations with div/jquery

瘦欲@ 提交于 2019-12-11 08:35:24

问题


So, I have a form on the page that I was using an input type="submit" to register users. After adding Data Annotations, some custom, some built-in, I wanted to customize the look of the input submit button only to find out that I cannot get it to match the other buttons on the page which are just divs/links. So, I would like to make it a div that uses jquery to submit the form and call of the data annotation's built-in and customized jquery before submitting. I tried this:

@using (Html.BeginForm("Index", "Register", FormMethod.Post, new { @id = "registerForm" })) { < div id="registerSubmit" class="divSubmit"> < a>Register< / a> < / div>}

$("#registerSubmit").onclick(function () { $("#registerForm").submit(); });

But, that is not working for me. Is there a way to submit and call the DA code correctly?


回答1:


This is what I use to achieve the very same thing

$(document).ready(function () {

    var submitButton = document.getElementById("registerSubmit");

    submitButton.onclick = function (event) {
        // do cool stuff before submit like validation or confirmations, whatever
        if ($(this).valid()) {
            // more cool stuff
            $("#registerForm").submit();
        }
        return false;
    };
});


来源:https://stackoverflow.com/questions/10593110/submit-mvc3-form-that-uses-data-annotations-with-div-jquery

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!