ASP.NET MVC表单提交带链接代替按钮 [英] ASP.NET MVC Form Submit with Link instead of Button

查看:288
本文介绍了ASP.NET MVC表单提交带链接代替按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想有一个简单的登录表单(或任何形式的这个问题),并能够发布它,而不是一个按钮,但有一个链接。

因此​​,没有输入,但 A

我见过许多解决方案,为这类问题,我尝试了很多不同的选择,每一次,没有任何反应。

我已经全部剥离JQuery的我得从什么,所以这是基地的情况:缺少了什么,使其提交表单

 <使用%(Html.BeginForm(登录,帐户,FormMethod.Post,新{ID =登录表单}))
  {%GT;
       <%= Html.TextBoxFor(X => x.UserName)%GT;< BR />
       <%= Html.TextBoxFor(X => x.Password)%GT;< BR />
       < A HREF =#ID =提交链接级=按钮>登录上述< / A>
<%}%GT;


解决方案

要做到这一点是使用JavaScript的唯一方法:

 <表ID =fooForm行动=/ foo的方法=后>
    < A HREF =#ID =submit_link级=按钮>登录上述< / A>
< /表及GT;

和在JavaScript:

  $(函数(){
    $('A#submit_link')。点击(函数(){
        $('#形式fooForm)提交()。
    });
});

虽然我会使用一个提交按钮建议你:

 <按钮式=提交>
    < A HREF =#ID =submit_link级=按钮>登录上述< / A>
< /按钮>

和尝试风采吧来看看作为锚。我的建议是始终使用提交按钮提交表单。这是更语义正确的,你还会发现,类似的事情pressing的<大骨节病>在编辑文本字段将提交这是一种原生的形式输入键。所以,做什么是语义正确,做造型的CSS。

I would like to have a simple login form (or any form for that matter), and be able to post it, not with a button, but with a link.

So no input, but a.

I've seen plenty of solutions for this type of question, and I've tried lots of different options, and every single time, nothing happens.

I've stripped all the JQuery from what I have, so this is the "base" situation: what's missing to make it submit the form?

<% using (Html.BeginForm("Login", "Account", FormMethod.Post, new { id = "loginForm" }))
  { %>
       <%= Html.TextBoxFor(x => x.UserName)%><br/>
       <%= Html.TextBoxFor(x => x.Password)%><br/>
       <a href="#" id="submit-link" class="button">Log In</a>
<%}%>

解决方案

The only way to make this happen is using javascript:

<form id="fooForm" action="/foo" method="post">
    <a href="#" id="submit_link" class="button">Log In</a>
</form>

and in javascript:

$(function() {
    $('a#submit_link').click(function() {
        $('form#fooForm').submit();
    });
});

Although I would suggest you using a submit button:

<button type="submit">
    <a href="#" id="submit_link" class="button">Log In</a>
</button>

And try to style it to look as an anchor. My suggestion is to always use submit buttons to submit forms. This is more semantically correct, you will also find that things like pressing the Enter key while editing a text field will submit the form which is kind of native. So do what's semantically correct and do the styling in CSS.

这篇关于ASP.NET MVC表单提交带链接代替按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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