是否可以将多个值保存到一个按钮?
传统上,按钮被设计为仅保存一个单个值,例如:
<button type="button" value="Love" onclick="fetchValue(this)"> Primary </button>
function fetchValue(_button){
alert(_button.value);
}
例如,在上面的HTML
代码中,当用户单击Primary按钮,就会弹出带有爱字样的提醒!
出于(UI/UX)用户界面/体验的目的,我需要设计一个可以容纳多个值而不仅仅是一个值的按钮。这是因为我计划将点击事件耦合到按钮以发送/发布所有存储的值。
非常想要的代码类似于下面的代码:
<button type="button" value="Love", value2="Love",
value3="Love" onclick="fetchValue(this)"> Primary </button>
是否有人知道如何解决这个问题,甚至知道如何更好地解决我的问题?
提前致谢
Traditionally buttons are designed to save only ONE single value, eg:
<button type="button" value="Love" onclick="fetchValue(this)"> Primary </button>
function fetchValue(_button){
alert(_button.value);
}
For instance, in the above HTML
code, when a user clicks on the Primary button, an alert with the word Love will pop up!
For (UI/UX)User Interface/Experience purposes, I need to design a button that can hold several values and NOT just one value. This is because I plan on coupling a click event to the button to send/post all stored values.
The much desired code would be something like the code below:
<button type="button" value="Love", value2="Love",
value3="Love" onclick="fetchValue(this)"> Primary </button>
Is there someone who might know how to resolve this or even a better solution to my problem?
Thanks in advance
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以将它们存储为数据集值
并在 js 中访问它们,例如
或者以 json 格式访问它们
并在 js 中访问它们,例如
You can either store them as dataset values
And access them in js like
Or access them in a json like format
And access them in js like
您可以使用
data
属性来获取其他值。You can use
data
attributes for additional values.您可以使用查找表来避免 HTML 混乱
,这里我也委托事件侦听器
You can use a lookup table to not clutter the HTML
Here I delegate event listeners too
您可以轻松地使用数据集为按钮设置多个值,而不是使用传统的值属性。如果您使用
data-attribute_name
那么您可以将所有数据集值作为单个 JS 对象访问,然后您可以从那里选择并重新格式化所有必需的值。例如如果您想同时获取所有呈现的值,您只需使用 up fetchValue() 函数内的 .map() 函数即可。
You can easily use the dataset to set multiple values to the button instead of using the traditional value attribute. if you use
data-attribute_name
then you can access all your dataset values as a single JS Object then you can choose and reformat all required values from there. For ExampleIf you want to get all value rendered togather you can simply use the
.map()
function inside the upfetchValue()
function.