[英]What is the best practice to store hidden values in web apps, without using the classic html form?
I hope that this question is not useless, but seriously.我希望这个问题不是无用的,而是认真的。 it is a problem for me .
这对我来说是个问题。
when I want to build a user interface in a web application to (add, delete,edit ) something I use HTML forms + hidden inputs
to store some values such as "user id"
to be deleted,edited..当我想在 web 应用程序中构建用户界面来(添加、删除、编辑)我使用 HTML forms +
hidden inputs
来存储some values such as "user id"
时。
I want to build the same interface using just " anchors + span + div
" insteand of " hidden inputs
".我想只使用“
anchors + span + div
”而不是“ hidden inputs
”来构建相同的界面。 when I write the javascript code that handles "onAdd,onDelete and onEdit" events I need to know what will be deleted?当我编写处理“onAdd、onDelete 和 onEdit”事件的 javascript 代码时,我需要知道什么会被删除?
what is the id that will be kicked out from my database
? what is the id that will be kicked out from my database
?
The question is, how to store this ID in my html tags without using hidden inputs?问题是,如何在不使用隐藏输入的情况下将此 ID 存储在我的 html 标签中?
what I'm using is something like this:我正在使用的是这样的:
<div id="userid_133421" >
<span>Name</span>
<a id="onDelete">Delete</a>
<a id="onEdit">Edit</a>
</div>
$("#onDelete").click(function({
user_id = $(this).parent().attr('id').substring($(this).attr('id').indexOf('_')+1);
$.post('index.php?component=user&action=remove&user_id=' + user_id, function(data){
});
}));
I checked some websites such as Dropbox , and I found that they don't use this technique to store values (such as file id), and even more they don't use classic hidden inputs to do that!我检查了一些网站,例如Dropbox ,我发现他们不使用这种技术来存储值(例如文件 id),甚至他们不使用经典的隐藏输入来做到这一点!
I just want to know how do you manage your code to choose the best way?我只想知道您如何管理代码以选择最佳方式? :(
:(
I second @nfecher.我第二个@nfecher。 That's how I am doing it right now in an application that I'm working on: We store the ids in the data-myAppName-itemTypeId.
这就是我现在在我正在处理的应用程序中执行此操作的方式:我们将 id 存储在 data-myAppName-itemTypeId 中。 The myAppName is creating a pseudo namespace to avoid collisions with other plugins that may use the same data-* attributes.
myAppName 正在创建一个伪命名空间,以避免与可能使用相同 data-* 属性的其他插件发生冲突。 eg, data-stackoverflow-commentId="4" No need for hidden inputs at all!!
例如,data-stackoverflow-commentId="4" 根本不需要隐藏输入!! You can follow the same style for all elements.
您可以对所有元素遵循相同的样式。 If it's a generic delete functionality, you could look up the parent class name and decided which data-* attribute you want to fetch!
如果它是通用删除功能,您可以查找父 class 名称并决定要获取哪个 data-* 属性!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.