HTML JQuery获取表单单选框选中的值
2023-09-27 14:29:24 时间
JQuery获取表单单选框选中的值
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<form name="a" id="a" action="#" method="post">
<input type="radio" name="num1" id="username" value="1"> 用户名1
<input type="radio" name="num1" id="passwd" value="2"> 用户名2
<input type="button" value="提交" onclick="click1()" />
</form>
</body>
<script>
function click1(){
var radio=document.getElementsByName("num1");
var selectvalue=null; // selectvalue为radio中选中的值
for(var i=0;i<radio.length;i++){
if(radio[i].checked==true) {
selectvalue=radio[i].value;
break;
}
}
console.log('selectvalue = ', selectvalue)
}
</script>
</html>
封装了函数:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<form name="a" id="a" action="#" method="post">
<input type="radio" name="num1" id="username" value="1" checked> 用户名1
<input type="radio" name="num1" id="passwd" value="2"> 用户名2
<input type="button" value="提交" onclick="click1()" />
</form>
</body>
<script>
function get_single_checked_val (name) {
var radio=document.getElementsByName(name);
var selectvalue=null; // selectvalue为radio中选中的值
for(var i=0;i<radio.length;i++){
if(radio[i].checked==true) {
selectvalue=radio[i].value;
break;
}
}
return selectvalue
}
function click1(){
var select_val1 = get_single_checked_val("num1")
console.log('select_val1 = ', select_val1)
}
</script>
</html>
console.log():
相关文章
- 教你三种jQuery框架实现元素显示及隐藏动画方式
- jQuery去除字符串中的空格、换行符、html标签样式方法
- jQuery和CSS 3定制HTML 5视频播放器
- HTML5中类jQuery选择器querySelector的使用
- jquery获取一个元素符合条件的第一个父元素
- html文件中jquery与velocity变量中的$冲突的解决方法
- jQuery Ajax方式上传文件实现暂停或取消上传
- jquery实现鼠标拖动
- A customized combobox with JQuery
- jQuery在html有效,在jsp无效的原因
- [置顶] 纯手工打造漂亮的垂直时间轴,使用最简单的HTML+CSS+JQUERY完成100个版本更新记录的华丽转身!
- ***浅析JQuery中的html(),text(),val()区别
- jquery如何获取第一个或最后一个子元素?
- jQuery中的text()、html()和val()以及innerText、innerHTML和value
- js jquery 实现html页面之间参数传递(单一参数、对象参数传递)
- 菜鸟教程-所有软件教学都有【python、java、c、c++、html、sql、css、jquery、bootstrap、vue、c#、go】
- jQuery html
- 使用 Sticky-Kit 实现基于 jQuery 的元素固定效果
- Jquery复习(四)之text()、html()、val()
- jQuery设计思想
- 基于jQuery点击图像居中放大插件Zoom
- jquery开发的”天才笨笨碰“游戏
- jquery重置html form