参考链接: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的设置与获取的更多相关文章

  1. asp.mvc获取checkbox、radio、select的值

    记录一下在asp.mvc中,提交表单时后台获取checkbox.radio.select值的方法. 1.获取select的值 <select name="type"> ...

  2. jq 操作radio,设置选中、获取选中值

    <label><input type="radio" name="sex" value="1">男</labe ...

  3. Jquery操作radio,checkbox,select表单操作实现代码

    一 .Select jQuery获取Select选择的Text和Value: 1. $("#select_id").change(function(){//code...}); / ...

  4. Jquery操作select、checkbox、radio详细讲解

    一 .Select jQuery获取Select选择的Text和Value: 1. $("#select_id").change(function(){//code...}); / ...

  5. 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 ...

  6. Jquery 操作Html 控件 CheckBox、Radio、Select 控件

    在使用 Javascript 编写前台脚本的时候,经常会操作 Html 控件,比如 checkbox.radio.select,用 Jquery 库操作其他会方便很多,下面用Jq对这些控件的操作进行一 ...

  7. html自定义checkbox、radio、select —— select篇

    上一篇<html自定义checkbox.radio.select —— checkbox.radio篇>介绍了我们是怎么将 html 自带的 checkbox.radio 改成我们自定义的 ...

  8. html自定义checkbox、radio、select —— checkbox、radio篇

    前些日子,所在公司项目的UI做了大改,前端全部改用 Bootstrap 框架,Bootstrap的优缺点在此就不详述了,网上一大堆相关资料. 前端的设计就交给我和另一个同事[LV,大学同班同学,毕业后 ...

  9. validate针对checkbox、radio、select标签的验证

    jQuery.validate 是jquery的一个插件,用来辅助开发者在客户端方便快捷的实现表单验证,最终达到提高用户体验的目的. 示例代码 <form id="formLogin& ...

随机推荐

  1. Android NDK 编译FFmpeg(不需要复杂的环境变量设置)

    环境: CentOS6.2——64位 借鉴:https://vec.io/posts/how-to-build-ffmpeg-with-android-ndk 在根目录下创建work文件夹:cd  / ...

  2. 遍历文件夹及其子文件夹下的.pdf文件,并解压文件夹下所有的压缩包

    List<PDFPATH> pdfpath = new List<PDFPATH>(); List<string> ziplist = new List<st ...

  3. VBA 简单调试

    在中断模式下(ctrl+Break键),可以做: 1.执行    工具----选项----编辑器----勾选“自动显示数据提示” 则当用鼠标悬停在变量或表达式上时,会出现提示窗口,显示其名称和值! 2 ...

  4. You raise me up

    You raise me up, so I can stand on mountains;You raise me up, to walk on stormy seas;I am strong, wh ...

  5. UnicodeEncodeError: 'latin-1' codec can't encode character 解决sae flask 中文问题

    #encoding=utf-8 #中文编码支持 import MySQLdb from flask import Flask, g, request app = Flask(__name__) app ...

  6. CentOS6.4下安装JDK1.6

    首先,切换到jdk安装包所在目录: [rot@centos6 Downloads]# ./jdk-6u45-linux-x64-rpm.bin Unpacking... Checksumming... ...

  7. wcf客户端 cookie

    public class CookieBehavior:IEndpointBehavior { private string _cookie; #region 构造函数 重载+2 public Coo ...

  8. ExtJS4.2学习(二)——入门基础

    1.工程的目录结构: src里放后台的文件,WebRoot里放前台的文件. index.html或者index.jsp等是整个项目的首页,在首页我们要引入ExtJS的CSS样式文件和ExtJS的核心类 ...

  9. python 学习之Windows 下的编码处理!

    问题1: Non-ASCII character '\xe9' in file 问题原因:程序编码上出现问题 解决方法:在程序头部加上代码 #-*- coding: UTF-8 -*- 设置代码编码为 ...

  10. 黑马程序员-- C语言执行过程及注意点

    通过对程序的逐步执行,了解C语言程序执行过程: 1.编写源文件即.c文件. #include <stdio.h> void play() { printf("hello worl ...