如何在js中选择具有相同类名的按钮列表中的单击按钮?

发布于 2025-01-15 17:32:33 字数 962 浏览 0 评论 0原文

我正在使用一些 javascript 代码,并且我有一个具有相同类名的按钮列表,但我想知道单击其中的哪个按钮才能获取值。我怎样才能用 vanilla js 做到这一点?这是示例代码

<button onclick="chooseRating()" id="rateBtn" class="rateBtn" value="1">1</button>
<button onclick="chooseRating()" id="rateBtn" class="rateBtn"value="2">2</button>
<button onclick="chooseRating()" id="rateBtn" class="rateBtn"value="3">3</button>
<button onclick="chooseRating()" id="rateBtn" class="rateBtn"value="4">4</button>
<button onclick="chooseRating()" id="rateBtn" class="rateBtn"value="5">5</button>

,我尝试的js代码是

const rateBtn = document.getElementsByClassName("rateBtn");
function chooseRating() {
    for (rate in rateBtn) {
        console.log(rateBtn[rate]);
        if (rateBtn[rate].clicked === true) {
            rating = rateBtn[rate].value;
            console.log(rating);
            //shows 5 don't know why
        }
    }
}

I was working with some javascript code and i have a list of buttons with the same class names but i wanted to know which button among them is clicked inorder to get the value. How can I do that with vanilla js? Here is the sample code

<button onclick="chooseRating()" id="rateBtn" class="rateBtn" value="1">1</button>
<button onclick="chooseRating()" id="rateBtn" class="rateBtn"value="2">2</button>
<button onclick="chooseRating()" id="rateBtn" class="rateBtn"value="3">3</button>
<button onclick="chooseRating()" id="rateBtn" class="rateBtn"value="4">4</button>
<button onclick="chooseRating()" id="rateBtn" class="rateBtn"value="5">5</button>

and the js code i tried is

const rateBtn = document.getElementsByClassName("rateBtn");
function chooseRating() {
    for (rate in rateBtn) {
        console.log(rateBtn[rate]);
        if (rateBtn[rate].clicked === true) {
            rating = rateBtn[rate].value;
            console.log(rating);
            //shows 5 don't know why
        }
    }
}

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

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

发布评论

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

评论(2

め可乐爱微笑 2025-01-22 17:32:33

您首先应该为每个按钮定义一个唯一的id

此外,您可以将 HTML 元素作为参数传递给 onClick 方法。

例如:

<button onclick="chooseRating(this)" id="rateBtn1" class="rateBtn" value="1">1</button>
<button onclick="chooseRating(this)" id="rateBtn2" class="rateBtn" value="2">2</button>
<button onclick="chooseRating(this)" id="rateBtn3" class="rateBtn" value="3">3</button>
<button onclick="chooseRating(this)" id="rateBtn4" class="rateBtn" value="4">4</button>
<button onclick="chooseRating(this)" id="rateBtn5" class="rateBtn" value="5">5</button>

然后您可以从普通的 javascript 代码获取 HTML 按钮的 ID 和值:

function chooseRating(e) {
    console.log(`my ID is ${e.id}, and my value is ${e.value}`);
}

You should first of all define a unique id per button.

Additionally, you can pass the HTML element to your onClick method as a parameter.

So for example:

<button onclick="chooseRating(this)" id="rateBtn1" class="rateBtn" value="1">1</button>
<button onclick="chooseRating(this)" id="rateBtn2" class="rateBtn" value="2">2</button>
<button onclick="chooseRating(this)" id="rateBtn3" class="rateBtn" value="3">3</button>
<button onclick="chooseRating(this)" id="rateBtn4" class="rateBtn" value="4">4</button>
<button onclick="chooseRating(this)" id="rateBtn5" class="rateBtn" value="5">5</button>

You can then from vanilla javascript code get the ID and the value of your HTML button:

function chooseRating(e) {
    console.log(`my ID is ${e.id}, and my value is ${e.value}`);
}
作死小能手 2025-01-22 17:32:33

您只需将变量传递给每个按钮中的函数调用即可。
例子:
onclick="myFunction(variable)"

HTML 文件:

   <button onclick="myFunction(value)" id="rateBtn" class="rateBtn" value="1">
    1
  </button>
  <button onclick="myFunction(value)" id="rateBtn" class="rateBtn" value="2">
    2
  </button>
  <button onclick="myFunction(value)" id="rateBtn" class="rateBtn" value="3">
    3
  </button>
  <button onclick="myFunction(value)" id="rateBtn" class="rateBtn" value="4">
    4
  </button>
  <button onclick="myFunction(value)" id="rateBtn" class="rateBtn" value="5">
    5
  </button>

JavaScript 文件:

function myFunction(val) { console.log(val); }

You can just pass on a variable to the function-call in each button.
EXAMPLE:
onclick="myFunction(variable)"

HTML-file:

   <button onclick="myFunction(value)" id="rateBtn" class="rateBtn" value="1">
    1
  </button>
  <button onclick="myFunction(value)" id="rateBtn" class="rateBtn" value="2">
    2
  </button>
  <button onclick="myFunction(value)" id="rateBtn" class="rateBtn" value="3">
    3
  </button>
  <button onclick="myFunction(value)" id="rateBtn" class="rateBtn" value="4">
    4
  </button>
  <button onclick="myFunction(value)" id="rateBtn" class="rateBtn" value="5">
    5
  </button>

JavaScript-file:

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