使用jQuery动态设置单选框的选中效果
2019/6/26 23:19:24
本文主要是介绍使用jQuery动态设置单选框的选中效果,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
一、需要实现的效果
这里使用jQuery来实现。需要实现的效果如下:当下拉条改变时,单选框选中的值随之变化。
<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title>动态设置单选框的选中</title> <!-- 作者:Harrison 时间:2018-12-05 描述:当下拉条改变时,动态的设置单选框的值 --> </head> <body> <select id="sexSelect" style="width: 10%;"> <option value="1">男</option> <option value="2">女</option> </select> 男:<input type="radio" value="1" name="sex" id="sexRadio1" checked/> 女:<input type="radio" value="2" name="sex" id="sexRadio2"/> </body> <script type="text/javascript" src="js/jquery-1.7.2.min.js" ></script> <script type="text/javascript"> $("#sexSelect").change(function(){ //获取选中的下拉条 var checkedOfSelect = $("#sexSelect").val(); //动态设置单选框的选中 if(checkedOfSelect == 1){ $("#sexRadio1").prop("checked",true); $("#sexRadio2").prop("checked",false); } if(checkedOfSelect == 2){ $("#sexRadio1").prop("checked",false); $("#sexRadio2").prop("checked",true); } }); </script> </html>
二、注意
?当设置单选框的checked属性时,要使用jQuery的prop()方法,不能够使用jQuery的attr()方法,attr()只适合简单html元素设置。
?jQuery的prop()方法,第二个参数为布尔值,不能设置成string类型:
正确:$("#sexRadio1").prop("checked",true);
错误:$("#sexRadio1").prop("checked","true");
总结
以上所述是小编给大家介绍的使用jQuery动态设置单选框的选中效果,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对找一找教程网网站的支持!
这篇关于使用jQuery动态设置单选框的选中效果的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-05-30React Native常用组件-点击组件
- 2024-05-30uniapp+vue3+uv-ui手机端后台OA管理模板
- 2024-05-29Python网络爬虫的时候json=就是让你少写个json.dumps()
- 2024-05-27React Native常用组件-展示组件
- 2024-05-27React Native常用组件-列表组件
- 2024-05-09vue3开发前端表单缓存自定义指令,移动端h5必备插件
- 2024-05-09React Hooks在class组件中的使用方式
- 2024-03-30[OIDC in Action] 2. 基于OIDC(OpenID Connect)的SSO(纯JS客户端)
- 2024-03-29terraform jsonencode
- 2024-03-13vuex-persist