checkbox、select、radio的设置与获取
参考链接:http://www.cnblogs.com/xiaopin/archive/2011/09/13/2175190.html
js版本:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
</head>
<body>
复选框:
<input type="checkbox" name="checkbox1" value="1" checked/>1
<input type="checkbox" name="checkbox1" value="2"/>2
<input type="checkbox" name="checkbox1" value="3" checked/>3
<input type="checkbox" name="checkbox1" value="4"/>4
<input type="checkbox" name="checkbox1" value="5"/>5 <br/> 单选框:
<input type="radio" id="radio1" name="radio1" value="1" checked>1
<input type="radio" id="radio2" name="radio1" value="2">2
<input type="radio" id="radio3" name="radio1" value="3">3
<br/>
下拉框:
<select id="select1">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
</select>
<br/> <button onclick="sub();">提交</button><br/>
<button onclick="fun1();">选择4,5复选框</button><button onclick="fun1_2()">全选</button><br/>
<button onclick="fun2();">选择3单选框</button><br/>
<button onclick="fun3();">选择2下拉框</button><br/>
<script type="text/javascript">
function fun1() {
//复选框设置值
var oCheckbox = document.getElementsByName("checkbox1");
var arr = [];
var index = 0;
for (var i = 0; i < oCheckbox.length; i++) {
if (oCheckbox[i].value == 4 || oCheckbox[i].value == 5) {
oCheckbox[i].checked = true;
} else {
oCheckbox[i].checked = false;
}
}
} function fun1_2() {
//复选框设置值
var oCheckbox = document.getElementsByName("checkbox1");
for (var i = 0; i < oCheckbox.length; i++) {
oCheckbox[i].checked = true;
}
} function fun2() {
//单选框设置值
var oRadio = document.getElementsByName("radio1");
for (var i = 0; i < oRadio.length; i++) {
if (oRadio[i].value == 3) {
oRadio[i].checked = true;
break;
}
}
} function fun3() {
//下拉框设置值
var oSelect = document.getElementById("select1");
oSelect.value = 2; //由option的text来选中选中
for (i = 0; i < oSelect.options.length; i++) {
alert("Element " + i + " is " + oSelect.options(i).text +
" and has the value " + oSelect.options(i).value);
}
oSelect.options(0).selected = true;
} function sub() {
//复选框获取值
var oCheckbox = document.getElementsByName("checkbox1");
var arr = [];
var index = 0;
for (var i = 0; i < oCheckbox.length; i++) {
if (oCheckbox[i].checked) {
arr[index++] = oCheckbox[i].value;
}
}
alert("复选框:" + arr.toString()); //单选框获取值
var oRadio = document.getElementsByName("radio1");
var rvalue = 0;
for (var i = 0; i < oRadio.length; i++) {
if (oRadio[i].checked) {
rvalue = oRadio[i].value;
break;
}
}
alert("单选框:" + rvalue); //下拉框获取值
var oSelect = document.getElementById("select1");
alert("下拉框:" + oSelect.value);
}
</script>
</body>
</html>
jQuery版本:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<script type="text/javascript" src="../js/jquery-1.10.2.js"></script>
</head> <body>
复选框:
<input type="checkbox" name="checkbox1" value="1" checked/>1
<input type="checkbox" name="checkbox1" value="2"/>2
<input type="checkbox" name="checkbox1" value="3" checked/>3
<input type="checkbox" name="checkbox1" value="4"/>4
<input type="checkbox" name="checkbox1" value="5"/>5 <br/> 单选框:
<input type="radio" id="radio1" name="radio1" value="1" checked>1
<input type="radio" id="radio2" name="radio1" value="2">2
<input type="radio" id="radio3" name="radio1" value="3">3
<br/>
下拉框:
<select id="select1">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
</select>
<br/> <button onclick="sub();">提交</button><br/>
<button onclick="fun1();">选择4,5复选框</button><br/>
<button onclick="fun2();">选择3单选框</button><br/>
<button onclick="fun3();">选择2下拉框</button><br/>
<script type="text/javascript">
$(document).ready(function () { });
function fun1() {
//复选框设置值
var oCheckbox = $("input[name=checkbox1]");
var arr = [];
var index = 0;
for (var i = 0; i < oCheckbox.length; i++) {
if (oCheckbox[i].value == 4 || oCheckbox[i].value == 5) {
oCheckbox[i].checked = true;
} else {
oCheckbox[i].checked = false;
}
}
} function fun2() {
//单选框设置值
var oRadio = $("input[name=radio1]");
for (var i = 0; i < oRadio.length; i++) {
if (oRadio[i].value == 3) {
oRadio[i].checked = true;
break;
}
}
} function fun3() {
//下拉框设置值
var oSelect = $("#select1");
oSelect.val(2);
} function sub() {
//复选框获取值
var oCheckbox = $("input[name=checkbox1]");
var arr = [];
var index = 0;
for (var i = 0; i < oCheckbox.length; i++) {
if (oCheckbox[i].checked) {
arr[index++] = oCheckbox[i].value;
}
}
alert("复选框:" + arr.toString()); //单选框获取值
var oRadio = $("input[name=radio1]");
var rvalue = 0;
for (var i = 0; i < oRadio.length; i++) {
if (oRadio[i].checked) {
rvalue = oRadio[i].value;
break;
}
}
alert("单选框:" + rvalue); //下拉框获取值
var oSelect = $("#select1");
alert("下拉框:" + oSelect.val());
}
</script>
</body>
</html>
checkbox、select、radio的设置与获取的更多相关文章
- asp.mvc获取checkbox、radio、select的值
记录一下在asp.mvc中,提交表单时后台获取checkbox.radio.select值的方法. 1.获取select的值 <select name="type"> ...
- jq 操作radio,设置选中、获取选中值
<label><input type="radio" name="sex" value="1">男</labe ...
- Jquery操作radio,checkbox,select表单操作实现代码
一 .Select jQuery获取Select选择的Text和Value: 1. $("#select_id").change(function(){//code...}); / ...
- Jquery操作select、checkbox、radio详细讲解
一 .Select jQuery获取Select选择的Text和Value: 1. $("#select_id").change(function(){//code...}); / ...
- Jquery 操作Html 控件 CheckBox、Radio、Select 控件 【转】http://www.cnblogs.com/lxblog/archive/2013/01/09/2853056.html
Jquery 操作Html 控件 CheckBox.Radio.Select 控件 在使用 Javascript 编写前台脚本的时候,经常会操作 Html 控件,比如 checkbox.radio ...
- Jquery 操作Html 控件 CheckBox、Radio、Select 控件
在使用 Javascript 编写前台脚本的时候,经常会操作 Html 控件,比如 checkbox.radio.select,用 Jquery 库操作其他会方便很多,下面用Jq对这些控件的操作进行一 ...
- html自定义checkbox、radio、select —— select篇
上一篇<html自定义checkbox.radio.select —— checkbox.radio篇>介绍了我们是怎么将 html 自带的 checkbox.radio 改成我们自定义的 ...
- html自定义checkbox、radio、select —— checkbox、radio篇
前些日子,所在公司项目的UI做了大改,前端全部改用 Bootstrap 框架,Bootstrap的优缺点在此就不详述了,网上一大堆相关资料. 前端的设计就交给我和另一个同事[LV,大学同班同学,毕业后 ...
- validate针对checkbox、radio、select标签的验证
jQuery.validate 是jquery的一个插件,用来辅助开发者在客户端方便快捷的实现表单验证,最终达到提高用户体验的目的. 示例代码 <form id="formLogin& ...
随机推荐
- #include <string>
1 append(string T&);字符串拼接 2 c_str string.c_str是Borland封装的String类中的一个函数,它返回当前字符串的首字符地址. 3 empty() ...
- javascript原生ajax;
<!doctype html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- 弹飞DZY(思维,打表,还没过全,先放着)
弹飞DZYDescription某天,机智的ZZC发明了一种超级弹力装置,为了在他的朋友DZY面前显摆,他邀请DZY一起玩个游戏.游戏一开始,ZZC在地上沿着一条直线摆上n个装置,每个装置设定初始弹力 ...
- hdu - 1757 - A Simple Math Problem
题意:当x < 10时, f(x) = x: 当x >= 10 时,f(x) = a0 * f(x-1) + a1 * f(x-2) + + a2 * f(x-3) + …… + a9 ...
- Java实现串口通信的小样例
用Java实现串口通信(windows系统下),须要用到sun提供的串口包 javacomm20-win32.zip.当中要用到三个文件,配置例如以下: 1.comm.jar放置到 JAVA_HOME ...
- SurfaceView 和 View 区别
android.view.View 和 android.view.SurfaceView SurfaceView 是从 View 基类中派生出来的显示类,直接子类有 GLSurfaceView和Vid ...
- JavaScript之insertBefore()和自定义insertAfter()的用法。
在JS图片库的第五版开发完后http://www.cnblogs.com/GreenLeaves/p/5691797.html#js_Five_Version我们发现一个问题,就是假设在图片列表之后还 ...
- ZOJ 1563 Pearls(动态规划)
/* 分析: 因为他给的数据是递增的 而求得是这些数据总的 最优解 所以我们可以考虑,它的子问题求解不影响总的求解 也就是我们可以先求出 第一个的最优解 第二个....以此类推到总的最优解 那么我们想 ...
- JavaScript知识(一)
首先想为大家分享两句话: 侧耳听智慧,专心求聪明,呼求明哲,扬声求聪明.——箴言2:2-3 你要保守你心,胜过保守一切,因为一生的果效,是由心发出.——箴言 4:23 ...O(∩_∩)O...今天学 ...
- shell检测interface是否已分配ip,qt调用shell脚本
#include <QCoreApplication>#include <QDebug>#include <QTextStream>#include <QDi ...