Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

203
Visualizações
how to count the number of times radio button is checked in react

if you click any of 3 radio button in any time it will count the number of clicked if the number of clicked is greater than 3 in console it give message you win or else you lose ,but its not working

import React, { useState } from 'react';
export default function App() {
    const [disable, setDisable] = useState(false);
    const check = ({target}) => {
    if (target.id==='one') {
      setDisable(true);
    }
    else {
      setDisable(false);
    }
    }
    const handlechange=(e)=>{
      let c=e.target.value;
      if(c.length>3)
      console.log('win')
      else
      console.log('lose')
    }
    return (
        <>
            <input  type="radio" onChange={(e) => {check(e);handlechange(e)}} disabled={disable} name="one" />
            <input type="radio" onChange={(e) => {check(e);handlechange(e)}} disabled={disable} name="one" />
            <input type="radio" onChange={(e) => {check(e);handlechange(e)}} disabled={disable} name="one" id="one"/>
       </>
    );
}
about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

Let the handlechange function know exactly whice input was checked: Please try this:

const handlechange=(e, number)=>{

      if(number>=3)
      console.log('win')
      else
      console.log('lose')
    }
    return (
        <>
            <input  type="radio" onChange={(e) => {check(e, 1);handlechange(e, 1)}} disabled={disable} name="one" />
            <input type="radio" onChange={(e) => {check(e, 2);handlechange(e, 2)}} disabled={disable} name="one" />
            <input type="radio" onChange={(e) => {check(e, 3);handlechange(e, 3)}} disabled={disable} name="one" id="one"/>
       </>
    );
about 4 years ago · Juan Pablo Isaza Relatório

0

Here is the solution to your problem, Create a clickCount state and update it on radio button change.

import React, { useState } from "react";
export default function App() {
  const [disable, setDisable] = useState(false);
  const [clickCount, setClickCount] = useState(1);

  const handlechange = (e) => {
    setClickCount(clickCount + 1);
    if (clickCount > 3) {
      console.log("win");
      setDisable(true);
    } else {
      console.log("lose");
    }
  };
  return (
    <>
      <input
        type="radio"
        onChange={handlechange}
        disabled={disable}
        name="one"
      />
      <input
        type="radio"
        onChange={handlechange}
        disabled={disable}
        name="one"
      />
      <input
        type="radio"
        onChange={handlechange}
        disabled={disable}
        name="one"
      />
    </>
  );
}

Sandbox link.

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda