Bootstrap 如何在JavaScript上禁用按钮时删除值

jljoyd4f  于 2022-12-07  发布在  Bootstrap
关注(0)|答案(1)|浏览(133)

I've two tables, non-clickable teams and clickable matches.
The tables image
The code on my GitHub

let teams = [
        {name: 'Roma', points: 0, wins: 0, draws: 0, loses: 0},
        {name: 'Atalanta', points: 0, wins: 0, draws: 0, loses: 0},
        {name: 'Milan', points: 0, wins: 0, draws: 0, loses: 0},
        {name: 'Fiorentina', points: 0, wins: 0, draws: 0, loses: 0},
    ]
let matches = [
        // Round 1.
        {homeName: teams[0].name, teamHome: 0, win: '', draw: '', awayTeamWin: '', teamAway: 1, awayName: teams[1].name},
        {homeName: teams[2].name, teamHome: 2, win: '', draw: '', awayTeamWin: '', teamAway: 3, awayName: teams[3].name},
        // Round 2.
        {homeName: teams[0].name, teamHome: 0, win: '', draw: '', awayTeamWin: '', teamAway: 2, awayName: teams[2].name},
        {homeName: teams[1].name, teamHome: 1, win: '', draw: '', awayTeamWin: '', teamAway: 3, awayName: teams[3].name},
        // Round 3.
        {homeName: teams[0].name, teamHome: 0, win: '', draw: '', awayTeamWin: '', teamAway: 3, awayName: teams[3].name},
        {homeName: teams[1].name, teamHome: 1, win: '', draw: '', awayTeamWin: '', teamAway: 2, awayName: teams[2].name},
    ]

Three functions one to add up points and wins.

function matchWinnerTeamPointsWins(matchWinner) {
        matchWinner.points += 3;
        matchWinner.wins ++;
}

Another to add losses.

function matchLoserTeamDefeats(matchLoser) {
        matchLoser.loses ++;
}

And to add points and draw.

function matchDrawPointsDraws(teamHome, teamAway) {
        teamHome.points ++;
        teamAway.points ++;
        teamHome.draws ++;
        teamAway.draws ++;
}

Then I create the matches table and add the click event.

function createMatchesTable(matchesTableBody, matches) {
        cleaner(matchesTableBody);
          let win = bodyRow.insertCell(-1);
          if (match.win === true) {
              win.className = 'bg-success';
          }
          win.onclick = (e) => {
              match.draw = '';
              match.awayTeamWin = '';
              match.win = !match.win;
              createMatchesTable(matchesTableBody, matches);
              matchWinnerTeamPoints(teams[match.teamHome]);
              matchLoserTeamPoints(teams[match.teamAway]);
              cleaner(createStandingTable(standingTableBody, teams));
          }

The idea is when the user click on home team win, draw or visitor team win I add the values on the teams, however when the user click again I return with the default values.
But what's happen is when the user click twice the values only sum and don't return.
For example:

let teams = [
        {name: 'Roma', points: 0, wins: 0, draws: 0, loses: 0},
    ]
// user click on home team win
let teams = [
        {name: 'Roma', points: 3, wins: 1, draws: 0, loses: 0},
    ]
// user click on home team win again
    // what's happen
let teams = [
        {name: 'Roma', points: 6, wins: 2, draws: 0, loses: 0},
    ]
    // what's expected
let teams = [
        {name: 'Roma', points: 0, wins: 0, draws: 0, loses: 0},
    ]
kgqe7b3p

kgqe7b3p1#

每次单击matchWinnerTeamPointsWins()时都调用它,这将增加对象的赢数和点数。如果要重置多次单击时的赢数和点数,可以在调用函数之前重置它,如下所示:

function createMatchesTable(matchesTableBody, matches) {
        cleaner(matchesTableBody);
        let win = bodyRow.insertCell(-1);
        if (match.win === true) {
            win.className = 'bg-success';
        }
        win.onclick = (e) => {
            if (match.played) {
              return;
            }
            match.draw = '';
            match.awayTeamWin = '';
            match.win = !match.win;
            createMatchesTable(matchesTableBody, matches);
            matchWinnerTeamPoints(teams[match.teamHome]);
            matchLoserTeamPoints(teams[match.teamAway]);
            cleaner(createStandingTable(standingTableBody, teams));
            match.played = true;
        }

相关问题