Anchor tag as submit button?

前端 未结 3 1066
面向向阳花
面向向阳花 2021-01-02 08:17

I\'m in the process of converting an existing website to use MVC and Entity Framework.

The layout / styling is all supposed to stay the same, and in order to follow

相关标签:
3条回答
  • 2021-01-02 09:03

    Just need to change Single line, For Optimization I think this one is a best way.

    @using (Html.BeginForm("Login", "Login", FormMethod.Post, new { id = "login", action = "Login" }))
    {
        // your HTML code
        <div class="spacer">
            <a href="" onclick='$("#login").submit()' class="login-button"></a>
        </div>
    }
    
    0 讨论(0)
  • 2021-01-02 09:11

    Typically you do not want to put a lot of javascript inside html tags, but if this only occurs once in your solution it will be fine. If however, there are anchor tags all over that need to post using Gokan's solution would work better with a marker class and some javascript in a common place.

    Change your anchor tag to look more like this:

    <a href="javascript:$('form').submit();" class="login-button"></a>
    
    0 讨论(0)
  • 2021-01-02 09:17

    I've made small changes

    HTML

    <form action="test.aspx" type="POST">
        <label>
            <span>Username</span>
            <input type="text" name="UserName" id="UserName" class="input-text required" />
        </label>
        <label>
            <span>Password</span>
            <input type="password" name="Password" id="Password" class="input-text required" />
        </label>
        <label>
            <input type="checkbox" name="RememberMe" id="RememberMe" class="check-box" />
            <span class="checkboxlabel">Remember Me</span>
        </label>
        <div class="spacer">
            <a href="javascript:void(0)" class="login-button">Login</a>
        </div>
    </form>
    

    jquery

    $(document).ready(function(){
       $(document).on("click",".login-button",function(){
         var form = $(this).closest("form");
         //console.log(form);
         form.submit();
       });
    });
    

    JSFiddle

    0 讨论(0)
提交回复
热议问题