0

我有一个登录/注册表单,每个功能都有一个按钮

<div id="ManualLogin">        
    @using (Html.BeginForm(new { Controller = "Companies", Action = "ManualLogOn" }))
    {            
        <fieldset>
            <legend>Enter details</legend>
            <div class="editor-label">
                @Html.LabelFor(u => u.Name)
            </div>
            <div class="editor-field">
                @Html.TextBoxFor(u => u.Name)                
            </div>
            <div class="editor-label">
                @Html.LabelFor(u => u.Password)
            </div>
            <div class="editor-field">
                @Html.PasswordFor(u => u.Password)                
            </div>

            <input type="submit" id="LogInButton" name="Command" value="Log in"/>
            <input type="submit" id="RegisterButton" name="Command" value="Register"/>

            <label id="ServerValidaiotnLabel">@ViewBag.Error</label>

        </fieldset>            
    }
</div>

如何更改它以使其根据单击的按钮调用操作。(登录/注册)

4

1 回答 1

1

您可以在按钮单击和手动提交表单时切换表单操作。这里示例:

表单标记的变化:

<buttom id="LogInButton" data-action="@Url.Action("login", "user")" class="Command">Log in</button>
<button id="RegisterButton" data-action="@Url.Action("register", "user")" class="Command"></button>

jQuery:

<script type="text/javascript">

        (function($){

        $('form button.Command').on('click', function() {
              var url = $(this).data('action');
              $(this).closest('form').attr('action', url).submit();
        });

        })(jQuery)

</script>
于 2013-06-27T14:19:11.123 回答