javascript - 如何选择多个ID并检查其是否具有相同的文本?

标签 javascript jquery html dom

function checkIfSomeoneWon() {
    if ($('#b1, #b2, #b3').html() === 'X')
    {
        alert('Someone Won!');
    }
}

我希望该函数仅在所有三个 ID 均包含字符“X”时才执行。

最佳答案

您可以在数组中设置这些值,然后使用 Array#Filter 检查它们是否都等于 X

function checkValues() {
  const values = [
    document.getElementById('b1').value,
    document.getElementById('b2').value,
    document.getElementById('b3').value,
  ];

  if (values.filter(a => a.toLowerCase() === 'x').length === values.length) {
    console.log('Someone won');
  }
}
<input id="b1"/>
<input id="b2"/>
<input id="b3"/>
<button onclick="checkValues()">Check values</button>

关于javascript - 如何选择多个ID并检查其是否具有相同的文本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45769586/

相关文章:

javascript - 如何使用 javascript_include_tag 获取绝对路径?

javascript - 如何在 Angular Material 中以日期选择器的格式显示正确的日期

javascript - JQuery:如何让元素在用户点击后显示?

javascript - 平滑滚动事件滚动

html - 如何使这个响应式 CSS 导航栏的子菜单全宽?

javascript - 将客户端值传递给服务器端

javascript - jQuery 在值更改之前触发更改

javascript - 如何使用 jquery 获取 html 5 多文件上传变量的值?

php - 登录时它没有带我到正确的位置

javascript - 如何使用 FileReader.readAsDataURL() 将图像的多个实例插入到 html 中?