我目前正在与一些成对的团队进行一个项目。单击一个团队会增加被单击团队的胜利,并增加与其链接的配对的损失。输掉两场的球队将被禁用,并将其文字更改为“被淘汰”。
我有一个重置按钮,应该清除所有计数器,理论上,通过清除计数器,禁用的按钮应该再次重新启用。然而,情况并非如此,它们保持禁用状态,并且文本切换回团队名称。重置后如何保持按钮启用状态?
任何意见将不胜感激:)
let groups = [{
name: "Chaos Coordinators",
wins: 0,
losses: 0
},
{
name: "Sofa King Awesome",
wins: 0,
losses: 0
},
{
name: "The Nerd Herd",
wins: 0,
losses: 0
},
{
name: "The Laughing Stock",
wins: 0,
losses: 0
},
{
name: "Brainy Bunch Gone Wild",
wins: 0,
losses: 0
},
{
name: "Cereal Killers",
wins: 0,
losses: 0
},
{
name: "The Mismatched Socks",
wins: 0,
losses: 0
},
{
name: "The Awkward Turtles",
wins: 0,
losses: 0
}
];
// Create pairs from the groups
function makeMatches(array) {
return array.map((_, i) => (i % 2 === 0) ? [array[i], array[i + 1]] : []).filter(v => v.length === 2);
}
let matches = makeMatches(groups);
// Create a map of team names to their index
const groupIndexMap = {};
groups.forEach((group, index) => {
groupIndexMap[group.name] = index;
});
// Function to handle button clicks
function handleButtonClick(groupName) {
const matchIndex = groupIndexMap[groupName];
const match = matches.find(match => match.some(group => group.name === groupName));
if (!match) return;
match.forEach((group, button) => {
if (group.name === groupName) {
group.wins += 1;
} else if (group.losses < 2) {
group.losses += 1;
}
});
updateButtonTexts();
console.log(groups);
}
// Function to update button texts
function updateButtonTexts() {
groups.forEach((group, index) => {
const button = document.getElementById(`button${index + 1}`);
if (button) {
button.textContent = `${group.name} - Wins: ${group.wins} - Losses: ${group.losses}`;
}
if (group.losses == 2) {
button.textContent = 'Eliminated';
button.disabled = true;
}
});
}
// Function to reset all counters
function resetCounters() {
groups.forEach(group => {
group.wins = 0;
group.losses = 0;
});
updateButtonTexts();
}
// Initialize button click handlers
function initializeButtons() {
groups.forEach((group) => {
const button = document.querySelector(`[data-team-name='${group.name}']`);
if (button) {
button.onclick = () => handleButtonClick(group.name);
}
});
// Initialize reset button handler
document.getElementById('resetButton').onclick = resetCounters;
// Initial update of button texts
updateButtonTexts();
}
// Initial render and setup
initializeButtons();
#group-buttons {
position: relative;
/* Container should be relative for positioning buttons absolutely */
width: 100%;
height: 500px;
/* Adjust height as needed */
border: 1px solid #ccc;
}
.group-button {
position: absolute;
/* Allows for free positioning */
cursor: pointer;
/* Indicates the element is clickable */
padding: 10px;
background: #f0f0f0;
border: 1px solid #ddd;
border-radius: 5px;
}
/* Example styles for individual buttons */
#button1 {
top: 50px;
left: 50px;
}
#button2 {
top: 50px;
left: 200px;
}
#button3 {
top: 150px;
left: 50px;
}
#button4 {
top: 150px;
left: 200px;
}
#button5 {
top: 250px;
left: 50px;
}
#button6 {
top: 250px;
left: 200px;
}
#button7 {
top: 350px;
left: 50px;
}
#button8 {
top: 350px;
left: 200px;
}
<button id="resetButton">Reset All Counters</button>
<div id="group-buttons">
<button id="button1" class="group-button" data-team-name="Chaos Coordinators">Chaos Coordinators - Wins: 0 - Losses: 0</button>
<button id="button2" class="group-button" data-team-name="Sofa King Awesome">Sofa King Awesome - Wins: 0 - Losses: 0</button>
<button id="button3" class="group-button" data-team-name="The Nerd Herd">The Nerd Herd - Wins: 0 - Losses: 0</button>
<button id="button4" class="group-button" data-team-name="The Laughing Stock">The Laughing Stock - Wins: 0 - Losses: 0</button>
<button id="button5" class="group-button" data-team-name="Brainy Bunch Gone Wild">Brainy Bunch Gone Wild - Wins: 0 - Losses: 0</button>
<button id="button6" class="group-button" data-team-name="Cereal Killers">Cereal Killers - Wins: 0 - Losses: 0</button>
<button id="button7" class="group-button" data-team-name="The Mismatched Socks">The Mismatched Socks - Wins: 0 - Losses: 0</button>
<button id="button8" class="group-button" data-team-name="The Awkward Turtles">The Awkward Turtles - Wins: 0 - Losses: 0</button>
</div>
当损失小于 2 时,您需要设置
button.disabled = true;
。这将在您重置计数器时重新启用该按钮。
function updateButtonTexts() {
groups.forEach((group, index) => {
const button = document.getElementById(`button${index + 1}`);
if (button) {
button.textContent = `${group.name} - Wins: ${group.wins} - Losses: ${group.losses}`;
if (group.losses == 2) {
button.textContent = 'Eliminated';
button.disabled = true;
} else {
button.disabled = false;
}
}
});
}
let groups = [{
name: "Chaos Coordinators",
wins: 0,
losses: 0
},
{
name: "Sofa King Awesome",
wins: 0,
losses: 0
},
{
name: "The Nerd Herd",
wins: 0,
losses: 0
},
{
name: "The Laughing Stock",
wins: 0,
losses: 0
},
{
name: "Brainy Bunch Gone Wild",
wins: 0,
losses: 0
},
{
name: "Cereal Killers",
wins: 0,
losses: 0
},
{
name: "The Mismatched Socks",
wins: 0,
losses: 0
},
{
name: "The Awkward Turtles",
wins: 0,
losses: 0
}
];
// Create pairs from the groups
function makeMatches(array) {
return array.map((_, i) => (i % 2 === 0) ? [array[i], array[i + 1]] : []).filter(v => v.length === 2);
}
let matches = makeMatches(groups);
// Create a map of team names to their index
const groupIndexMap = {};
groups.forEach((group, index) => {
groupIndexMap[group.name] = index;
});
// Function to handle button clicks
function handleButtonClick(groupName) {
const matchIndex = groupIndexMap[groupName];
const match = matches.find(match => match.some(group => group.name === groupName));
if (!match) return;
match.forEach((group, button) => {
if (group.name === groupName) {
group.wins += 1;
} else if (group.losses < 2) {
group.losses += 1;
}
});
updateButtonTexts();
//console.log(groups);
}
// Function to update button texts
function updateButtonTexts() {
groups.forEach((group, index) => {
const button = document.getElementById(`button${index + 1}`);
if (button) {
button.textContent = `${group.name} - Wins: ${group.wins} - Losses: ${group.losses}`;
if (group.losses == 2) {
button.textContent = 'Eliminated';
button.disabled = true;
} else {
button.disabled = false;
}
}
});
}
// Function to reset all counters
function resetCounters() {
groups.forEach(group => {
group.wins = 0;
group.losses = 0;
});
updateButtonTexts();
}
// Initialize button click handlers
function initializeButtons() {
groups.forEach((group) => {
const button = document.querySelector(`[data-team-name='${group.name}']`);
if (button) {
button.onclick = () => handleButtonClick(group.name);
}
});
// Initialize reset button handler
document.getElementById('resetButton').onclick = resetCounters;
// Initial update of button texts
updateButtonTexts();
}
// Initial render and setup
initializeButtons();
#group-buttons {
position: relative;
/* Container should be relative for positioning buttons absolutely */
width: 100%;
height: 500px;
/* Adjust height as needed */
border: 1px solid #ccc;
}
.group-button {
position: absolute;
/* Allows for free positioning */
cursor: pointer;
/* Indicates the element is clickable */
padding: 10px;
background: #f0f0f0;
border: 1px solid #ddd;
border-radius: 5px;
}
/* Example styles for individual buttons */
#button1 {
top: 50px;
left: 50px;
}
#button2 {
top: 50px;
left: 200px;
}
#button3 {
top: 150px;
left: 50px;
}
#button4 {
top: 150px;
left: 200px;
}
#button5 {
top: 250px;
left: 50px;
}
#button6 {
top: 250px;
left: 200px;
}
#button7 {
top: 350px;
left: 50px;
}
#button8 {
top: 350px;
left: 200px;
}
<button id="resetButton">Reset All Counters</button>
<div id="group-buttons">
<button id="button1" class="group-button" data-team-name="Chaos Coordinators">Chaos Coordinators - Wins: 0 - Losses: 0</button>
<button id="button2" class="group-button" data-team-name="Sofa King Awesome">Sofa King Awesome - Wins: 0 - Losses: 0</button>
<button id="button3" class="group-button" data-team-name="The Nerd Herd">The Nerd Herd - Wins: 0 - Losses: 0</button>
<button id="button4" class="group-button" data-team-name="The Laughing Stock">The Laughing Stock - Wins: 0 - Losses: 0</button>
<button id="button5" class="group-button" data-team-name="Brainy Bunch Gone Wild">Brainy Bunch Gone Wild - Wins: 0 - Losses: 0</button>
<button id="button6" class="group-button" data-team-name="Cereal Killers">Cereal Killers - Wins: 0 - Losses: 0</button>
<button id="button7" class="group-button" data-team-name="The Mismatched Socks">The Mismatched Socks - Wins: 0 - Losses: 0</button>
<button id="button8" class="group-button" data-team-name="The Awkward Turtles">The Awkward Turtles - Wins: 0 - Losses: 0</button>
</div>