我有一个性别单选按钮和类(class)下拉按钮,
我在 Jquery 中有动态值。
如何获得性别和类(class)中的选定选项。
这是我的 Fiddle Demo
我也在这里提供我的代码:
HTML:
Male <input type="radio" name="gender" id="male" value="1"> -
Female <input type="radio" name="gender" id="female" value="2">
<br>
<select name="courses" id="courses">
<option value="0">Choose Courses</option>
<option value="1">PHP</option>
<option value="2">Jquery</option>
<option value="3">HTML</option>
</select>
JS:
$(function() {
//these are the values I got, these values are dynamic
var gender = 2;
var course = 3;
}
最佳答案
获取
$("input[type='radio']").val();// get radio box value
$("#courses").val();// get dropdown with id "courses value
设置
$("input[type='radio'][value=2]").attr("checked","checked");// set radio box value with checked propety
$("#courses").val(3);// set dropdown with id "courses value
你也可以通过改变值来获取值
$("input[type='radio'],#courses").change(function(){
alert($(this).val());
});
参见 UPDATED DEMO
关于javascript - 使用 Jquery 获取选中的单选按钮和选定的下拉值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19675320/