简体   繁体   English

根据单选按钮更改值

[英]Change a value depending on the Radio button

I wanted your help on a question / problem I have.我需要您的帮助来解决我遇到的问题。

I am making a quote calculator.我正在制作报价计算器。

What he does is enter an amount and then he throws you the discount you are going to get.他所做的是输入一个金额,然后他向您抛出您将获得的折扣。

The problem I have is that I have 2 radio buttons and those have different value (one is 99%, the other 98%).我的问题是我有 2 个单选按钮,它们具有不同的值(一个是 99%,另一个是 98%)。

Then, in a specific way to qualify, the discount that it throws has to be made differently.然后,以特定的方式获得资格,它抛出的折扣必须有所不同。

   <input id="valor" type="text" onkeyUp="calcular();">

This input is to enter the amount这个输入是输入金额

 <label for="">Casa</label>
    <input type="radio" name="r" value="99" id="">
    <label for="">Negocio</label>
    <input type="radio" name="r" value="98" id="">



<span id="total"></span>

Here you throw the discount.在这里你打折扣。

var valor = document.getElementById("valor").value;

var result= document.getElementById('result');
var descuento = parseInt(valor)*0.90;

Here I make the formula for the discount but this is where I don't know how to spend the value depending on which checkbox.在这里,我制定了折扣公式,但这是我不知道如何根据复选框花费价值的地方。

//Add the result to the DOM
result.innerHTML = 'Ahorro de: $' + descuento;
    <label for="">Casa</label>
    <input type="radio" name="r" value="99" id="" onclick="check(99)">
    <label for="">Negocio</label>
    <input type="radio" name="r" value="98" id="" onclick="check(98)">
<span id="total"></span>

<script>
function check(percentage){
    console.log(percentage)
        var valor = document.getElementById("valor").value;
        var result= document.getElementById('total');
        var descuento = parseInt(valor)*percentage/100;
        result.innerHTML = 'Ahorro de: $' + descuento;
}
</script>

You can try this.你可以试试这个。

 let valor = document.getElementById("valor"); let result = document.getElementById('total'); let calcular = () => { var radVal = parseInt(document.querySelector(".ab:checked").value); var descuento = parseInt(valor.value) * radVal / 100; result.innerHTML = 'Ahorro de: $' + descuento; }
 This input is to enter the amount: <input id="valor" type="text" onkeyUp="calcular();"> <label for="a">Casa</label> <input type="radio" name="r" value="99" id="a" class="ab" checked onChange="calcular();"> <label for="b">Negocio</label> <input type="radio" name="r" value="98" id="b" class="ab" onChange="calcular();"> <input id="calc" type="submit" value="calc"> <span id="total"></span>

Maybe Like this:也许像这样:

 function calk(){ var r_val = document.querySelectorAll("[name='r']:checked")[0].value; r_val = parseInt(r_val, 10); var descuento = r_val*0.90; document.getElementById("total").innerHTML = 'Ahorro de: $' + descuento.toFixed(2); } var r_els = document.querySelectorAll("[name='r']"); for (var i = 0; i < r_els.length; i++) { r_els[i].addEventListener("click", function() { calk(); }); }
 <label for="">Casa <input type="radio" name="r" value="99" /> </label> <label for="">Negocio <input type="radio" name="r" value="98" /> </label></br> <span id="total"></span>

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

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