根据单击的按钮更改表单提交URL [英] Change form submission URL based on button clicked

查看:55
本文介绍了根据单击的按钮更改表单提交URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用 button 作为到另一个页面的链接.我环顾四周,阅读了一些解决方案,但没有一个奏效.我不想在我的 form 标记中使用 action ,因为我可能想将几个按钮作为该 form 标记中的链接.

i want to use a button as a link to another page. i have looked around and read some solutions but none worked. i dont want to use action in my form tag because i might want to have couple of buttons as links in that form tag.

这是我最后尝试过的方法:(功课)

here is what i have tried last:(didnt work)

<button onclick="location.href='../ClientSide/Registration/registration.aspx'">register</button>

我在做什么错?还是有更好/其他的方法?

what am i doing wrong? or is there a better/other way?

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

i really would like to use only html if possible, if not then to use: javascript or asp.net( i dont know jquery or php)

推荐答案

您不能仅使用HTML直接进行 .

You cannot do this directly using only HTML.

您有两个选择:

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

Option 1 Post the data to a single script on the server that decides what to do based on which button is clicked.

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

然后,位于/some-url.aspx 的脚本将根据 button_action 的值来决定下一步要做什么.

Then your script at /some-url.aspx would decide what to do next based on the value of button_action.

选项2 使用JavaScript可以根据单击的按钮来更改 form action 属性.

Option 2 Use JavaScript to change the form's action attribute based on which button is clicked.

<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并有些混乱.这实际上取决于您要在哪里获得额外的逻辑以及您对表单的可访问性的感觉.

Option 1 is more accessible but requires some messiness on the server side. Option 2 is fairly clean but requires JavaScript and a little messiness to work. It really depends on where you want the extra logic and how you feel about the accessibility of your form.

这篇关于根据单击的按钮更改表单提交URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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