注册前接受模式

时间:2018-05-06 21:11:32

标签: javascript jquery asp.net-mvc bootstrap-modal

当用户尝试注册时,我希望在接受注册之前弹出一些模式。在用户接受模态上显示的术语之前,一切似乎都有效。当用户按下按钮(submitBtn2)时,它没有做出反应。我已经尝试过使用闹钟进行测试,看看我的提示是否是问题,但即使没有显示警报 - 我做错了什么?

注册表

@using (Html.BeginForm("Register", "Account", FormMethod.Post, new { @class = "form-horizontal", role = "form" }))
{
    <div align="center" class="w3-black w3-opacity" id="register">
        <br />
        @Html.AntiForgeryToken()
        <h4>Register</h4>
        <hr />
        @Html.ValidationSummary("", new { @class = "text-danger" })
        <div class="form-group" id="lastname">
            @Html.LabelFor(m => m.Email, new { @class = "control-label" })
            <div>
                @Html.TextBoxFor(m => m.Email, new { @class = "form-control" })
            </div>
        </div>
        <div class="form-group">
            @Html.LabelFor(m => m.Password, new { @class = "control-label" })
            <div>
                @Html.PasswordFor(m => m.Password, new { @class = "form-control" })
            </div>
        </div>
        <div class="form-group">
            @Html.LabelFor(m => m.ConfirmPassword, new { @class = "control-label" })
            <div>
                @Html.PasswordFor(m => m.ConfirmPassword, new { @class = "form-control" })
            </div>
        </div>
        <div class="form-group">
            <button class="w3-button w3-light-grey w3-section" type="submit" value="Register">
                <i class="fa fa-user"></i>Create user
            </button>
            <input type="button" name="ConfirmRegBtn" value="Submit" id="submitBtn" data-toggle="modal" data-target="#confirm-submit" class="w3-button w3-light-grey w3-section" />
        </div>
    </div>
}

JQuery的

$('#submitBtn2').click(function () {
    /* when the submit button in the modal is clicked, submit the form */
    window.alert("sometext");
    //$('#Register').submit();
});

模态

    <!--legal data-stuff-->
<div class="modal fade" id="confirm-submit" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header w3-center">
                <h1>Before you create your user</h1>
            </div>
            <div class="modal-body scroll">
                Some stuff from database
                Lorem ipsum...
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">Cancel</button>
                <a href="#" id="submitBtn2" class="btn btn-success success">Submit</a>
            </div>
        </div>
    </div>
</div>

1 个答案:

答案 0 :(得分:2)

您需要为表单提供Id。您可以在BeginForm辅助方法的htmlAttributes参数中指定它。

@using (Html.BeginForm("Register", "Account", FormMethod.Post,
                 new { @class = "form-horizontal", role = "form" , @id= "Register" }))
{
   <!-- Your form elements/existing code goes here -->
}

这将生成ID属性值为Register

的表单标记
<form action="/" class="form-horizontal" id="Register" method="post" role="form">
       <!-- Your form elements  goes here -->
</form>

在jQuery选择器中使用此Id属性值,获取form元素并提交表单。

$(document).ready(function () {
    $('#submitBtn2').click(function () {
        $('#Register').submit();
    });
});

这是一种做法。另一种方法是将模态对话框保留在表单内,单击模态中的按钮时,找到最接近的容器form标记并提交它。 jQuery closest方法会很方便。但Id选择器会更快(希望比o(n)解决方案更好)