Html 根据单击的按钮更改表单提交URL

Html 根据单击的按钮更改表单提交URL,html,button,Html,Button,我想使用按钮作为指向其他页面的链接。我环顾四周,读了一些解决方案,但没有一个奏效。我不想在我的表单标记中使用操作,因为我可能希望在表单标记中有两个按钮作为链接 这是我上次试过的:(没用) 寄存器 我做错了什么?还是有更好的方法 如果可能的话,我真的只想使用html,如果不想,那么就使用:javascript或asp.net(我不知道jquery或php)在您的页面上使用jquery和下面的代码 $(function(){ $("button").on("click",function(e

我想使用
按钮
作为指向其他页面的链接。我环顾四周,读了一些解决方案,但没有一个奏效。我不想在我的
表单
标记中使用
操作
,因为我可能希望在
表单
标记中有两个按钮作为链接

这是我上次试过的:(没用)

寄存器
我做错了什么?还是有更好的方法


如果可能的话,我真的只想使用html,如果不想,那么就使用:javascript或asp.net(我不知道jquery或php)

在您的页面上使用jquery和下面的代码

$(function(){
   $("button").on("click",function(e){
      e.preventDefault();
      location.href='../ClientSide/Registration/registration.aspx';
   })
});

e、 preventDefault()使表单不提交

一个简单的答案是将按钮包装在锚内

<a href='../ClientSide/Registration/registration.aspx'>
<button>Click Here</button>
</a>

您不能仅使用HTML直接执行此操作

您有两个选择:

选项1将数据发布到服务器上的单个脚本中,该脚本根据单击的按钮决定要执行的操作

<form action="/some-url.aspx" method="post">
  <button name="button_action" value="register">register</button>
  <button name="button_action" value="another">another</button>
</form>
<form id="form-with-buttons" action="/some-url" method="post">
  <button id="register-button" data-url="/some-url.aspx">register</button>
  <button id="another-button" data-url="/another-url.aspx">another</button>
</form>

<script>
  $("#register-button, #another-button").click(function(e) {
    e.preventDefault();

    var form = $("#form-with-buttons");

    form.prop("action", $(this).data("url"));
    form.submit();
  });
</script>

选项1更容易访问,但需要服务器端的一些混乱选项2相当干净,但需要JavaScript和一点混乱才能工作。这实际上取决于您想要额外逻辑的位置以及您对表单可访问性的感受。

使用
上的
formaction=“url”
标记,如下所示:

如果可能,我真的希望只使用html单击=“return false;location.href='url'”这不会将表单数据发布到服务器。它只是将用户重定向到另一个页面。那么你想把表单数据发布到每个按钮应该指向的URL吗?是的,很明显你说的他想要没有js和jquery@ArsenIbragimov因此,选择1。我给了他选择,并为每一个选择想出了理由。非常感谢!你能给我看一个脚本的例子吗?/some url.aspx“@Cooper不,我不知道.NET。我是一名Ruby开发人员。:)在Ruby中,我会这样做:
case参数[:button\u action]当'register'做某事时,当'other'做某事时,其他引发ActiveRecord::RecordNotFound end
<form id="form-with-buttons" action="/some-url" method="post">
  <button id="register-button" data-url="/some-url.aspx">register</button>
  <button id="another-button" data-url="/another-url.aspx">another</button>
</form>

<script>
  $("#register-button, #another-button").click(function(e) {
    e.preventDefault();

    var form = $("#form-with-buttons");

    form.prop("action", $(this).data("url"));
    form.submit();
  });
</script>