简体   繁体   English

如何在 JavaScript 中添加和删除值 onclick 事件

[英]How to add and remove value onclick event in JavaScript

I've two tables one with objects and their values, other with options to the user click that change the another table values.我有两个表,其中一个带有对象及其值,另一个带有用户单击以更改另一个表值的选项。

Image of the tables桌子的图像

If the user click one time, the values change correctly, however, if the user click again, the values won't change correctly.如果用户单击一次,则值会正确更改,但是,如果用户再次单击,则值将不会正确更改。

For example:例如:

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},
    ]
function matchWinnerTeamPoints(matchWinner) {
        matchWinner.points += 3;
        matchWinner.wins ++;
}
function matchLoserTeamPoints(matchLoser) {
        matchLoser.loses ++;
}
function matchDrawPoints(teamHome, teamAway) {
        teamHome.points ++;
        teamAway.points ++;
        teamHome.draws ++;
        teamAway.draws ++;
}
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));
          }

Once the user click on win cell the team get 3 points and 1 win, but if the user click again the team will receive more 3 points and 1, but I want to remove the 3 points and 1 win, however I don't know how.一旦用户点击胜利单元格,球队获得3分1胜,但如果用户再次点击,球队将获得更多3分1胜,但我想删除3分1胜,但我不知道如何。

so the same button does two different mutation, is there a pattern to it?所以同一个按钮做了两个不同的突变,它有模式吗? best to have two buttons & functions, one for add, one for remove.最好有两个按钮和功能,一个用于添加,一个用于删除。 but you can make it with one method by supplying an argument with either negative or positive values.但是您可以通过提供带有负值或正值的参数来使用一种方法。 depending on action取决于行动

eg例如

function matchWinnerTeamPoints(match Winner, p, w) {
        matchWinner.points += p;
        matchWinner.wins += w;
    

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM