本文介绍了单击单选按钮时如何输出价格?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

<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>

有人单击单选按钮时知道如何输出价格吗?假设如果我选择小",它将显示输出Rm2 ...例如Price = Rm 2....我不知道如何继续...如果有人帮我解决这个问题,我将不胜感激. .谢谢〜

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 ~

推荐答案

您可以在计算函数(这是指元素)中作为参数this传递.

You can pass as parameter this in calculate function(this refers to the element).

使用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>

使用普通的 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>

这篇关于单击单选按钮时如何输出价格?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-13 14:25