简体   繁体   English

单击单选按钮时如何输出价格?

[英]How to output the price when clicking the radio button?

 <fieldset style="width:240 px"> <legend>Food order</legend> <fieldset style="width:240 px"> <legend>Nasi Lemak</legend> <p> <input type="radio" name="j" value="2" onclick="calculate();" /> <label for='small' class="inlinelabel"> Small</label> (RM2) </p> <p> <input type="radio" name="j" value="3" onclick="calculate();" /> <label for='regular' class="inlinelabel"> Regular</label> (RM3) </p> <p> <input type="radio" name="j" value="4" onclick="calculate();" /> <label for='large' class="inlinelabel"> Large</label> (RM4) </p> <tr> <td>Price = </td> <td><output type="number" name="price" id="output"></output></td> </tr> </fieldset> <script type="text/javascript"> calculate() { } </script> 

Does anyone know how to output the price when clicking the radio button ? 单击单选按钮时,有人知道如何输出价格吗? Let say if i choose Small then it will display the output Rm2...for example Price = Rm 2....i have no idea how to continue it...i will very appreciate it if someone help me solve this...thanks ~ 假设如果我选择“小”,它将显示Rm2的输出...例如Price = Rm 2。...我不知道如何继续...如果有人帮我解决这个问题,我将不胜感激。 。谢谢〜

You can pass as parameter this in calculate function(this refers to the element). 可以传递作为参数this在计算功能(这是指该元素)。

Using jquery: 使用jQuery:

 function calculate(obj) { $("output").text(obj.value); } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <fieldset style="width:240 px"> <legend>Food order</legend> <fieldset style="width:240 px"> <legend>Nasi Lemak</legend> <p> <input type="radio" name="j" value="2" onclick="calculate(this);" /> <label for='small' class="inlinelabel"> Small</label> (RM2) </p> <p> <input type="radio" name="j" value="3" onclick="calculate(this);" /> <label for='regular' class="inlinelabel"> Regular</label> (RM3) </p> <p> <input type="radio" name="j" value="4" onclick="calculate(this);" /> <label for='large' class="inlinelabel"> Large</label> (RM4) </p> <tr> <td>Price =</td> <td> <output type="number" name="price" id="output"></output> </td> </tr> </fieldset> 

Using plain js : 使用普通的js

 function calculate(obj) { document.getElementById("output").innerHTML = obj.value; } 
 <fieldset style="width:240 px"> <legend>Food order</legend> <fieldset style="width:240 px"> <legend>Nasi Lemak</legend> <p> <input type="radio" name="j" value="2" onclick="calculate(this);" /> <label for='small' class="inlinelabel"> Small</label> (RM2) </p> <p> <input type="radio" name="j" value="3" onclick="calculate(this);" /> <label for='regular' class="inlinelabel"> Regular</label> (RM3) </p> <p> <input type="radio" name="j" value="4" onclick="calculate(this);" /> <label for='large' class="inlinelabel"> Large</label> (RM4) </p> <tr> <td>Price =</td> <td> <output type="number" name="price" id="output"></output> </td> </tr> </fieldset> 

Try it 试试吧

document.getElementsByName("j").addEventListener("change", function(){
      document.getElementById("price").innerHTML = this.value;// Show the value in output element

      //or

    var myRadioValue= this.value; //for some calculate
});

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

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