有条件地跳过提交的表单字段,减少 URL 混乱
我有多个隐藏的表单字段,它们存储有关当前视图的值(例如,如果某些情况下,通常隐藏的 div 可见等),以便在表单回发时恢复布局。
问题是我总是提交所有这些隐藏字段,即使它们是默认的,在此过程中生成大量不必要的 URL 混乱(例如 http://www.example.com/view?ab=&ac=&ad= 等)。
我宁愿仅提交实际影响视图的字段(也就是说,没有指定的默认值),以便将 URL 混乱降至最低。
我尝试手动删除/插入输入,但这是一场噩梦。有更好的方法吗?
I have multiple hidden form fields which store values about the the current view (e.g. if certain, normally hidden div's are visible etc.) to restore the layout when the form posts back.
The problem is that I'm always submitting all these hidden fields, even if they are in default, generating lots of unnecessary URL clutter in the process (e.g. http://www.example.com/view?ab=&ac=&ad= and so on).
I'd rather submit only the fields which are actually influencing the view (meaning, don't have a specified default value) so that the URL clutter is at a minimum.
I tried manually deleting/inserting input's but its a nightmare. Is there a better way to do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
使用 jQuery,您可以在传输之前
remove()
这些表单元素。我能想到的另一种方法是删除name 属性
。Using jQuery, you might just
remove()
those form elements before transmitting. Another way I could think of is to remove thename attribute
.尝试禁用它们-
“被禁用的控制不可能成功。” -- http://www.w3.org/ TR/html401/interact/forms.html#h-17.13.2
Try disabling them-
"Controls that are disabled cannot be successful." -- http://www.w3.org/TR/html401/interact/forms.html#h-17.13.2
除非您有理由将这些选项保留在 URL 中,否则为什么不将这些值存储在 cookie 中呢?事实上,许多网站经常这样做。如果您想更加小心,可以将显示选项存储在每个用户的数据库中,但这是您的选择。
Unless you have a reason for keeping these options in the URL, why not store those values in a cookie? In fact, this is often done on many sites. If you want to be more careful, the display options can be stored in a database for each user, but that's your choice.