如何根据单选按钮选择获取用户输入? [英] How to get user input based on radio button selection?
问题描述
我有一个简单的程序,我可以选择某些材料。我在我的程序中声明了一个名为cost的变量。因此,根据用户选择的材料,变量应该被赋予不同的值。例如。
I have a simple program where I give the option to select certain materials. I have one variable declared in my program called cost. So depending on which materials the user selects, the variable should be assigned a different value. For instance.
if('#radio-choice-1) is selected{
var cost = 5;
}
else if('#radio-choice-2') is selected{
var cost = 10;
}
有两种材料选择。例如,如果你选择材料A,那么成本就是一个值,如果你选择材料B,那么成本值就是另一个值。
There are 2 material choices. Both have different $ value for example so say if you pick material A then cost is one value and if you pick material B then cost value is another value.
这是一个 FIDDLE 为了让事情更轻松而创建。
This is a FIDDLE I created to make things easier.
推荐答案
在您的html中:
in your html you have:
<fieldset data-role="controlgroup">
<input type="radio" name="radio-choice" id="radio-choice-1" value="choice-1">
<label for="radio-choice-1">SS316L</label>
<input type="radio" name="radio-choice" id="radio-choice-2" value="choice-2">
<label for="radio-choice-2">HDPE</label>
</fieldset>
您可以将成本置于值中
属性如下:
you could put the cost in the value
attribute like this:
<fieldset data-role="controlgroup">
<input type="radio" name="radio-choice" id="radio-choice-1" value="5.00">
<label for="radio-choice-1">SS316L</label>
<input type="radio" name="radio-choice" id="radio-choice-2" value="8.25">
<label for="radio-choice-2">HDPE</label>
</fieldset>
并使用jquery获取选定项目的成本,如下所示:
and use jquery to grab the cost of the selected item like this:
var cost = $('input[name=radio-choice]:checked').val();
下面是这种技术的一个工作示例: http://jsfiddle.net/RhnvU/
here's a working example of this technique: http://jsfiddle.net/RhnvU/
这篇关于如何根据单选按钮选择获取用户输入?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!