提交带有锚点的表单 + javascript - 不好的做法?

发布于 2024-09-10 15:44:42 字数 315 浏览 10 评论 0原文

我目前有两个锚标记发送 url 查询以将选票放入数据库中,但我想切换到使用表单来保持 url 干净并避免重复查询(不是真正的问题,但使其看起来很难看)。

现在锚点需要在内部包含一个span标签,以通过css向按钮(链接)添加额外的背景元素,因此我不能使用常规的表单按钮。

您是否认为使用 javascipt 从锚点提交这些表单是不好的做法?我可以想到其他一些解决方案,但它们似乎都不值得麻烦。我是否应该保留一个无脚本回退,让禁用 javascript 的用户可以发送 url 查询(我真的想完全避免 url 查询)?如果,那么如何最好地执行呢?

谢谢,
西蒙.

I currently have two anchor tags sending url queries to put votes inside a database, but i want to switch to using forms to keep the urls clean and avoid duplicate queries (not really an issue, but makes it look ugly).

Now the anchors need to contain a span-tag inside to add additional background elements to the buttons (links) through css, therefore I can't use regular form-buttons.

Do you consider submitting these forms from the anchors with javascipt as bad practice? I can think of some other solutions, but they all doesn't seem worth the trouble. Should I keep a no-script fallback where users with javascript disabled get to send url queries instead (i really want to avoid url queries altogether)? If, then how would this best be executed?

Thanks,
Simon.

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

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

发布评论

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

评论(3

怕倦 2024-09-17 15:44:42

您可以在表单中使用

您可以使其看起来与现在的 完全相同,但单击它将提交一个 POST 表单。就好像我们有一个可以进行 POST 操作的

you can use <button> in your form. it can contain content, and you can style it as you like.

you can make it look exactly as the <a> you have now, but clicking it will submit a POST form. It is almost as if we have a <a> that does POST!

旧伤还要旧人安 2024-09-17 15:44:42

以这种方式制作按钮应该可行:

<button type="submit" value="upvote">
  <span>Vote Up</span>
</button>

<button type="submit" value="downvote">
  <span>Vote Down</span>
</button>

您应该尽量不要使用 JavaScript 提交表单,或者至少确保该功能在关闭时可用。

Doing your buttons this way should work:

<button type="submit" value="upvote">
  <span>Vote Up</span>
</button>

<button type="submit" value="downvote">
  <span>Vote Down</span>
</button>

You should endeavor to never submit a form with JavaScript, or at the very least, ensure that the functionality is available when it is turned off.

哽咽笑 2024-09-17 15:44:42

我建议使用普通表单提交按钮。
您可以将 CSS 应用于按钮以使其看起来时尚或应用背景图像。

我不认为你的做法是不好的做法。但对于使用 No-Script for Firefox 等附加组件的用户来说,该表单将会被破坏。

I would recommend using the normal form submit button.
You can apply CSS to the button to make it look stylish or apply a background image.

I do not think the way you did it is a bad-practice. But the form will break for users who use add-ons like No-Script for Firefox.

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