根据用户输入更改表单操作

发布于 2024-10-05 21:05:27 字数 181 浏览 0 评论 0原文

我有这样的形式:

<form action"url.aspx">
</form>

用户必须能够选择专业或非专业。如果用户选择 pro,则必须选择 action="url1.aspx" 如果用户选择 pro,则必须选择 action="url2.aspx"。

I have this form:

<form action"url.aspx">
</form>

The user has to be able to choose pro or nonpro. If the user chooses pro it has to choose action="url1.aspx" If the user chooses pro it has to choose action="url2.aspx".

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

七七 2024-10-12 21:05:28

我同意查理格里弗的回答。这是最简单有效的方法。

我不同意 GolezTrol 的观点,即“最好总是发布到同一页面并且......”仅当您需要执行一些常见的后处理操作时,那么可能但仍然如此你最好为此调用一个可重用的函数。作为初学者,您可能会坚持您认为简单的方法,但这并不意味着它是最好的。

重定向到错误页面
我再次不同意这一点“通过使用 JavaScript,您将允许用户在某些情况下将数据发布到错误的页面。”
如果由于你的逻辑错误而进入错误的页面(在某些复杂的情况下,你没有正确认为会出现),那么这是你的错,你只是通过忽略 javascript 来隐藏你的错误,并且由于以下原因会出现更多的延迟多个页面重定向、可以避免的不必要的页面加载等。

在所有入口点对所有参数进行正确验证(变量类型检查、强制参数检查)非常重要。如果确实发生了发布到错误页面的情况,这将保证安全。

在没有 JavaScript 的情况下
所有现代浏览器都支持 JavaScript。如果用户已关闭,则会显示一条消息,但这种情况发生的情况较少。可以向用户显示一条消息“为了更好的体验/易于使用,请打开 javascript”。
这里最好的方法是使用服务器端逻辑(提交到相同的 URL,然后重定向)来确保表单正常工作。所以通常在大多数情况下,用户会获得更好的 UI。

初学者忽略此类事情或在某些地方遵循而不在其他地方遵循是很常见的做法。正确且最大限度地重用代码是解决大多数问题的关键。是的,准备最可重用的代码需要时间,但花在这里的时间总会在以后得到回报。

I agree with charliegriefer's answer. That's is the simplest and efficient way.

I do not agree with GolezTrol's point that "it will be always best to post to the same page and the ..." Only if you need to do some common post processing action, then may be but still you should better call a reusable function for that. As a beginner you may stick to what you feel is easy but that does not mean it is best.

Redirecting to wrong page
I don't agree again to this point "By using JavaScript, you will allow the user under some circumstances to post data to the wrong page."
If it goes to wrong page due to your logic error (in some complex cases which you did not properly think would come up) then it is your fault and you will just be hiding your mistake by ignoring javascript and there will be more delays due to multiple page redirects, unecessary page loads which could be avoided etc.

It is a very important point to follow proper validation of all parameters at all entry points (variable type checking, mandatory parameters checking). That will guarantee security in case of posting to wrong pages, if it happens at all.

In the absence of javascript
All modern browsers support javascript. If user has turned off, a message can be displayed but that would happen in fewer cases. Users can be shown a message "For better experience/ease of usage turn on javascript".
The best approach here would be keeping a provision for the form to work normally using server side logic (submitting to the same url and then redirecting). So normally and in most cases, users will get better UI.

It is a very common practise among beginners to ignore such things or follow in some places and not in others. Proper and maximum possible code reuse is the key to majority of problems. Yes it takes time to prepare the most reusable code but time spent here always pays off later.

你的他你的她 2024-10-12 21:05:27

最好始终发布到同一页面,并让该页面根据“专业”或“非专业”的选择来决定执行哪些代码。优点:不易出错。我认为即使没有 JavaScript,您的表单也应该始终正常运行,尽管您可以使用 JavaScript 使其更可用。通过使用 JavaScript,您将允许用户在某些情况下将数据发布到错误的页面。

It would be best to always post to the same page and let that page decide which code to execute based on the choice of 'pro' or 'nonpro'. Advantage: Less error prone. I think your form should always function properly, even without javascript, although you can use JavaScript to make it more usable. By using JavaScript, you will allow the user under some circumstances to post data to the wrong page.

少女净妖师 2024-10-12 21:05:27

您可以通过 JavaScript/jQuery 操作表单的“action”属性

$( your-form ).attr( 'action', 'url1.aspx' );
$( your-form ).attr( 'action', 'url2.aspx' );

因此,无论用户使用什么机制来选择“pro”或“nonpro”(您没有提及,我也没有看到任何代码),操作操作适当地表达形式的属性。

You can manipulate the "action" attribute of a form via JavaScript/jQuery

$( your-form ).attr( 'action', 'url1.aspx' );
$( your-form ).attr( 'action', 'url2.aspx' );

So whatever mechanism the user is using to choose "pro" or "nonpro" (you don't mention and I don't see any code), manipulate the action attribute of the form appropriately.

阪姬 2024-10-12 21:05:27

您可以根据您的选择隐藏和显示 div,这将是最简单的方法

you can hide and show divs based on your choice, that would be simplest way to do that

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文