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

345
Visualizações
How to make onchange work on radio input?

I have an input field where the value will change automatically when an option is selected. How to write code so that it works the same way when the radio input option is selected?

function changeIncreaseMode() {
  let selectMode = $("#selectMode").val();
  let multiplier = $("#multiplier").val();
  
  if(selectMode == 'usingMultiplier'){
    var increase = (multiplier/100) + 1;
  } else {
    var increase = (multiplier - 1) * 100;
  }

  $("#multiplier").val(parseFloat(increase).toFixed(2));  
        
}
<!DOCTYPE html>
<html>
  <body>
    <div class="form-group">
      <label for="multiplier">Increase on loss:</label> 
      <input type="text" class="form-control" id="multiplier" value="2" placeholder="Enter multiplier" pattern="^\d*(\.\d{0,8})?$" required />
      <select id="selectMode" onchange="changeIncreaseMode();">
        <option value="usingMultiplier">X</option>
        <option value="usingPercentage">%</option>
      </select>
    </div>
    
    <!-- increase using multiplier or percentage -->
    <div class="custom-control custom-radio custom-control-inline small">
      <input type="radio" id="usingMultiplier" name="usingMultiplier" class="custom-control-input" checked>
      <label class="custom-control-label" for="usingMultiplier">using multiplier <i class="fas fa-times"></i></label>
    </div>
    <div class="custom-control custom-radio custom-control-inline small">
      <input type="radio" id="usingPercentage" name="usingMultiplier" class="custom-control-input">
      <label class="custom-control-label" for="usingPercentage">using percentage <i class="fas fa-percentage"></i></label>
    </div>
    <!-- end increase using multiplier or percentage -->
    
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script> 
  </body>
</html>

and then i have also prepared a code that will calculate and display the results when the submit button is clicked. the code snippet looks like this :

base_bet = multiplier * base_bet; //increase loss using multiplier/first-code

base_bet = multiplier / 100 * base_bet + base_bet; //increase loss using percentage/second-code

How to make when selected the first option then the calculation uses the first code, if selected the second option then the calculation uses the second code?

I'm very grateful before and after.

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

I will be pleasure if this code can help you.

function changeIncreaseMode() {
  let selectMode = $("#selectMode").val();
  let multiplier = $("#multiplier").val();
  
  if(selectMode == 'usingMultiplier'){
    var increase = (multiplier/100) + 1;
  } else {
    var increase = (multiplier - 1) * 100;
  }

  $("#multiplier").val(parseFloat(increase).toFixed(2));    
}

function changeIncreaseModeByRadio() {
  const isMultiplier = $("#usingMultiplier").is(':checked');
  const isPercentage = $("#usingPercentage").is(':checked');
  const multiplier = $("#multiplier").val();
  
  let increase = 0;
  if(isMultiplier){
    increase = (multiplier/100) + 1;
  }
  else if(isPercentage){
    increase = (multiplier - 1) * 100;
  }

  $("#multiplier").val(parseFloat(increase).toFixed(2));    
}
<!DOCTYPE html>
<html>
  <body>
    <div class="form-group">
      <label for="multiplier">Increase on loss:</label> 
      <input type="text" class="form-control" id="multiplier" value="2" placeholder="Enter multiplier" pattern="^\d*(\.\d{0,8})?$" required />
      <select id="selectMode" onchange="changeIncreaseMode();">
        <option value="usingMultiplier">X</option>
        <option value="usingPercentage">%</option>
      </select>
    </div>
    
    <!-- increase using multiplier or percentage -->
    <div class="custom-control custom-radio custom-control-inline small">
      <input type="radio" id="usingMultiplier" name="usingMultiplier" class="custom-control-input" onChange="changeIncreaseModeByRadio();" checked>
      <label class="custom-control-label" for="usingMultiplier">using multiplier <i class="fas fa-times"></i></label>
    </div>
    <div class="custom-control custom-radio custom-control-inline small">
      <input type="radio" id="usingPercentage" name="usingMultiplier" class="custom-control-input" onChange="changeIncreaseModeByRadio();">
      <label class="custom-control-label" for="usingPercentage">using percentage <i class="fas fa-percentage"></i></label>
    </div>
    <!-- end increase using multiplier or percentage -->

<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script> 
  </body>
</html>

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