是否可以将多个值保存到一个按钮?

发布于 2025-01-12 08:29:45 字数 680 浏览 2 评论 0原文

传统上,按钮被设计为仅保存一个单个值,例如:

<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 技术交流群。

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

发布评论

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

评论(4

断桥再见 2025-01-19 08:29:46

您可以将它们存储为数据集值

<button value="love" data-value1="hate" data-value2="happy" onclick="fetchvalue(this)" >click me</button>

并在 js 中访问它们,例如

function fetchvalue(_button){
alert(_button.value)
alert(_button.dataset.value1)
alert(_button.dataset.value2)
}

或者以 json 格式访问它们

<button value={value1:"hate",value2:"happy" } onclick="fetchvalue(this)" >click me</button>

并在 js 中访问它们,例如

function fetchvalue(_button){
alert(_button.value.value1)
alert(_button.value.value2)

}

You can either store them as dataset values

<button value="love" data-value1="hate" data-value2="happy" onclick="fetchvalue(this)" >click me</button>

And access them in js like

function fetchvalue(_button){
alert(_button.value)
alert(_button.dataset.value1)
alert(_button.dataset.value2)
}

Or access them in a json like format

<button value={value1:"hate",value2:"happy" } onclick="fetchvalue(this)" >click me</button>

And access them in js like

function fetchvalue(_button){
alert(_button.value.value1)
alert(_button.value.value2)

}
束缚m 2025-01-19 08:29:46

您可以使用 data 属性来获取其他值。

function fetchValue(_button){  
  alert(`${_button.value} ${_button.dataset.value2}`);
}
<button type="button" value="Love" data-value2="you" onclick="fetchValue(this)"> Primary </button>

You can use data attributes for additional values.

function fetchValue(_button){  
  alert(`${_button.value} ${_button.dataset.value2}`);
}
<button type="button" value="Love" data-value2="you" onclick="fetchValue(this)"> Primary </button>

旧话新听 2025-01-19 08:29:46

您可以使用查找表来避免 HTML 混乱

,这里我也委托事件侦听器

const values = {
  "Love": ["Adore", "Be fond of"],
  "Hate": ["Dislike", "Despise"]
};
document.getElementById("buttons").addEventListener("click", function(e) {
  const button = e.target.closest("button");
  if (button.matches(".fetchbutton")) console.log(values[button.value]);
})
<div id="buttons">
  <button type="button" value="Love" class="fetchbutton"> Love </button>
  <button type="button" value="Hate" class="fetchbutton"> Hate </button>
</div>

You can use a lookup table to not clutter the HTML

Here I delegate event listeners too

const values = {
  "Love": ["Adore", "Be fond of"],
  "Hate": ["Dislike", "Despise"]
};
document.getElementById("buttons").addEventListener("click", function(e) {
  const button = e.target.closest("button");
  if (button.matches(".fetchbutton")) console.log(values[button.value]);
})
<div id="buttons">
  <button type="button" value="Love" class="fetchbutton"> Love </button>
  <button type="button" value="Hate" class="fetchbutton"> Hate </button>
</div>

沙沙粒小 2025-01-19 08:29:46

您可以轻松地使用数据集为按钮设置多个值,而不是使用传统的值属性。如果您使用 data-attribute_name 那么您可以将所有数据集值作为单个 JS 对象访问,然后您可以从那里选择并重新格式化所有必需的值。例如

function fetchValue(_button){
  // To get all value data as JS Object use _button.dataset
  console.log("JS Object Data:\r\n")
  console.log(_button.dataset)
  
  // To get all value data as JSON use JSON.stringify(_button.dataset)
  console.log("\r\n\r\nJSON Data:\r\n")
  console.log(JSON.stringify(_button.dataset))
  
  // To access the specific value you can use _button.dataset.name_here from data-name_here
  console.log("\r\n\r\nSpecific Data:\r\n")
  console.log(_button.dataset.value1)  
  console.log(_button.dataset.value2)
}
<button data-value1="Something 1" data-value2="Something 2" data-value3="Something 3" onclick="fetchValue(this)">
Click Me
</button>

如果您想同时获取所有呈现的值,您只需使用 up fetchValue() 函数内的 .map() 函数即可。

let allValues = _button.dataset.map((value) => {
  return `${value} `;
});

console.log(`All values are: ${allValues}`);

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 Example

function fetchValue(_button){
  // To get all value data as JS Object use _button.dataset
  console.log("JS Object Data:\r\n")
  console.log(_button.dataset)
  
  // To get all value data as JSON use JSON.stringify(_button.dataset)
  console.log("\r\n\r\nJSON Data:\r\n")
  console.log(JSON.stringify(_button.dataset))
  
  // To access the specific value you can use _button.dataset.name_here from data-name_here
  console.log("\r\n\r\nSpecific Data:\r\n")
  console.log(_button.dataset.value1)  
  console.log(_button.dataset.value2)
}
<button data-value1="Something 1" data-value2="Something 2" data-value3="Something 3" onclick="fetchValue(this)">
Click Me
</button>

If you want to get all value rendered togather you can simply use the .map() function inside the up fetchValue() function.

let allValues = _button.dataset.map((value) => {
  return `${value} `;
});

console.log(`All values are: ${allValues}`);

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