<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>
&#13;
点击单选按钮有人知道如何输出价格吗?假设我选择Small然后它将显示输出Rm2 ...例如Price = Rm 2 ....我不知道如何继续它...如果有人帮我解决这个问题,我将非常感激.. 。谢谢〜
答案 0 :(得分:4)
您可以在计算函数中传递参数this
(这是指元素)。
使用jquery:
function calculate(obj) {
$("output").text(obj.value);
}
&#13;
<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>
&#13;
使用普通的 js :
function calculate(obj) {
document.getElementById("output").innerHTML = obj.value;
}
&#13;
<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>
&#13;
答案 1 :(得分:0)
试试吧
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
});