当我计算选中的框时,它会加上一个框

我的网络应用程序是关于体育比赛的预测。我的页面显示了所有比赛以及您可以从一场比赛的每个结果中获得的分数(马德里 = 12 分 VS 巴塞罗那 = 15 分)。因此,用户在匹配项中选中一个框并为他选择正确的结果。我希望每次用户选中一个框时,向他显示他选中的框数。


这是我的 Javascript 来计算选中的框:


    const updateCount = function() {

      var x = document.querySelectorAll(".square:checked").length;

      document.querySelector(".plus").innerHTML = x;

    };

这是将显示选中框数量的 HTML


<div class=" d-flex pt-2">

            <h3 class="typos">Matchs pronostiqués</h3>

            <h3 class="typos pts" style="font-weight: bold; padding-left: 5px;"><%= current_user.forecasts.where(confirmed: true, season_id: Season.last.id).count %>/50</h3>

            <span class="plus"></span>

          </div>

这是我的 Javascript,以便了解用户预测的游戏以及他选择的结果:


const selectOutcome = () => {

  const selects = document.querySelectorAll(".square");

  selects.forEach((outcome)=>{

    outcome.addEventListener("click",(event) => {

  $('input[type="checkbox"]').on('change', function() {

   $(this).siblings('input[type="checkbox"]').not(this).prop('checked', false);

});

      const result = event.currentTarget.dataset.outcome;

      console.log(result);

      const id = event.currentTarget.parentNode.dataset.id;

      console.log(id);

      const box = event.currentTarget.checked;

      console.log(box);

      const url = 'store_outcome?result='+result+'&match='+id+'&box='+box

      fetch(url)

        .then(response => response.json())

        .then((data) => {

      console.log(data);

      });

    });

  });

}


const validePanier = () => {

  const panier = document.getElementById('panier');

  panier.addEventListener("click", (event) =>{

    console.log("click")

    const player = document.getElementById('season_player').value;

    fetch('confirm_pending?player='+player)

    .then(response => response.json())

    .then((data) => {

    console.log(data);

    });

  })

}

有一个数据ID。目的是当用户选中一个框时,我可以获得匹配的 ID,以便为正确的游戏创建正确的预测。


哆啦的时光机
浏览 103回答 1
1回答

梵蒂冈之花

我会委托而不是使用内联事件处理程序在这里我计算复选框 - 为什么你不想要选中的 RADIO 的值?注意我把所有的火柴都包裹在<div id="matches">...</div>document.getElementById("matches").addEventListener("change", function(e) {&nbsp; const tgt = e.target;&nbsp; if (tgt.classList.contains("square")) {&nbsp; &nbsp; const parent = tgt.closest(".displaysquares");&nbsp; &nbsp; var x = parent.querySelectorAll(".square:checked").length;&nbsp; &nbsp; document.querySelector(".plus").innerHTML += parent.dataset.id + ": " + x +"<br/>" ;&nbsp; }})<span class="plus"></span><div id="matches">&nbsp; <div class="d-flex justify-content-center mb-2 mt-2">&nbsp; &nbsp; <h4 class="typopo">League 1</h4>&nbsp; </div>&nbsp; <div class="d-flex justify-content-center mb-2 mt-2">&nbsp; &nbsp; <h3 class="tit">&nbsp; &nbsp; &nbsp; Some date&nbsp; &nbsp; </h3>&nbsp; &nbsp; <h3 class="typopo pl-2">-</h3>&nbsp; &nbsp; <h3 class="typopo pl-2">Some string</h3>&nbsp; </div>&nbsp; <div class="d-flex flex-row justify-content-around mb-4 mt-4" data-id="MATCH 1">&nbsp; &nbsp; <div class="d-flex flex-column align-items-center col-4">&nbsp; &nbsp; &nbsp; <div class="row">&nbsp; &nbsp; &nbsp; &nbsp; <h3 class="typopo">TEAM LOGO </h3>&nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; &nbsp; <div class="row text-align-center">&nbsp; &nbsp; &nbsp; &nbsp; <h3 class="tit">Some other team string</h3>&nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; </div>&nbsp; &nbsp; <div class="d-flex flex-column justify-content-center">&nbsp; &nbsp; &nbsp; <p class="typopo text-center">VS</p>&nbsp; &nbsp; &nbsp; <div class="d-flex flex-row align-items-center col-4">&nbsp; &nbsp; &nbsp; &nbsp; <div class="displaysquares" data-id="MATCH 1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <input type="checkbox" class="square" data-outcome="1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <input type="checkbox" class="square" data-outcome="NULL">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <input type="checkbox" class="square" data-outcome="2">&nbsp; &nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; </div>&nbsp; </div>&nbsp; <div class="d-flex justify-content-center mb-2 mt-2">&nbsp; &nbsp; <h4 class="typopo">League 2</h4>&nbsp; </div>&nbsp; <div class="d-flex justify-content-center mb-2 mt-2">&nbsp; &nbsp; <h3 class="tit">&nbsp; &nbsp; &nbsp; Some date&nbsp; &nbsp; </h3>&nbsp; &nbsp; <h3 class="typopo pl-2">-</h3>&nbsp; &nbsp; <h3 class="typopo pl-2">Some string</h3>&nbsp; </div>&nbsp; <div class="d-flex flex-row justify-content-around mb-4 mt-4" data-id="MATCH 2">&nbsp; &nbsp; <div class="d-flex flex-column align-items-center col-4">&nbsp; &nbsp; &nbsp; <div class="row">&nbsp; &nbsp; &nbsp; &nbsp; <h3 class="typopo">TEAM LOGO </h3>&nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; &nbsp; <div class="row text-align-center">&nbsp; &nbsp; &nbsp; &nbsp; <h3 class="tit">Some other team string</h3>&nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; </div>&nbsp; &nbsp; <div class="d-flex flex-column justify-content-center">&nbsp; &nbsp; &nbsp; <p class="typopo text-center">VS</p>&nbsp; &nbsp; &nbsp; <div class="d-flex flex-row align-items-center col-4">&nbsp; &nbsp; &nbsp; &nbsp; <div class="displaysquares" data-id="MATCH 2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <input type="checkbox" class="square" data-outcome="1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <input type="checkbox" class="square" data-outcome="NULL">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <input type="checkbox" class="square" data-outcome="2">&nbsp; &nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; </div>&nbsp; </div></div>改用收音机const matches = document.getElementById("matches")matches.addEventListener("change", function(e) {&nbsp; const tgt = e.target;&nbsp; if (tgt.classList.contains("square")) {&nbsp; &nbsp; var x = [...matches.querySelectorAll(".square:checked")].map(chk => chk.closest(".displaysquares").dataset.id + ": "+chk.dataset.outcome)&nbsp; &nbsp; document.querySelector(".plus").innerHTML = x.join("<br/>");&nbsp; }})<span class="plus"></span><div id="matches">&nbsp; <div class="d-flex justify-content-center mb-2 mt-2">&nbsp; &nbsp; <h4 class="typopo">League 1</h4>&nbsp; </div>&nbsp; <div class="d-flex justify-content-center mb-2 mt-2">&nbsp; &nbsp; <h3 class="tit">&nbsp; &nbsp; &nbsp; Some date&nbsp; &nbsp; </h3>&nbsp; &nbsp; <h3 class="typopo pl-2">-</h3>&nbsp; &nbsp; <h3 class="typopo pl-2">Some string</h3>&nbsp; </div>&nbsp; <div class="d-flex flex-row justify-content-around mb-4 mt-4" data-id="MATCH 1">&nbsp; &nbsp; <div class="d-flex flex-column align-items-center col-4">&nbsp; &nbsp; &nbsp; <div class="row">&nbsp; &nbsp; &nbsp; &nbsp; <h3 class="typopo">TEAM LOGO </h3>&nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; &nbsp; <div class="row text-align-center">&nbsp; &nbsp; &nbsp; &nbsp; <h3 class="tit">Some other team string</h3>&nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; </div>&nbsp; &nbsp; <div class="d-flex flex-column justify-content-center">&nbsp; &nbsp; &nbsp; <p class="typopo text-center">VS</p>&nbsp; &nbsp; &nbsp; <div class="d-flex flex-row align-items-center col-4">&nbsp; &nbsp; &nbsp; &nbsp; <div class="displaysquares" data-id="MATCH 1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <input type="radio" name="outcome1" class="square" data-outcome="1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <input type="radio" name="outcome1" class="square" data-outcome="NULL">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <input type="radio" name="outcome1" class="square" data-outcome="2">&nbsp; &nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; </div>&nbsp; </div>&nbsp; <div class="d-flex justify-content-center mb-2 mt-2">&nbsp; &nbsp; <h4 class="typopo">League 2</h4>&nbsp; </div>&nbsp; <div class="d-flex justify-content-center mb-2 mt-2">&nbsp; &nbsp; <h3 class="tit">&nbsp; &nbsp; &nbsp; Some date&nbsp; &nbsp; </h3>&nbsp; &nbsp; <h3 class="typopo pl-2">-</h3>&nbsp; &nbsp; <h3 class="typopo pl-2">Some string</h3>&nbsp; </div>&nbsp; <div class="d-flex flex-row justify-content-around mb-4 mt-4" data-id="MATCH 2">&nbsp; &nbsp; <div class="d-flex flex-column align-items-center col-4">&nbsp; &nbsp; &nbsp; <div class="row">&nbsp; &nbsp; &nbsp; &nbsp; <h3 class="typopo">TEAM LOGO </h3>&nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; &nbsp; <div class="row text-align-center">&nbsp; &nbsp; &nbsp; &nbsp; <h3 class="tit">Some other team string</h3>&nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; </div>&nbsp; &nbsp; <div class="d-flex flex-column justify-content-center">&nbsp; &nbsp; &nbsp; <p class="typopo text-center">VS</p>&nbsp; &nbsp; &nbsp; <div class="d-flex flex-row align-items-center col-4">&nbsp; &nbsp; &nbsp; &nbsp; <div class="displaysquares" data-id="MATCH 2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <input type="radio" name="outcome2" class="square" data-outcome="1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <input type="radio" name="outcome2" class="square" data-outcome="NULL">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <input type="radio" name="outcome2" class="square" data-outcome="2">&nbsp; &nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; </div>&nbsp; </div></div>
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript