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/