使用jQuery为文本框、单选框、多选框、下拉框、下拉多选框设值及返回值的处理
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>使用jQuery为文本框、单选框、多选框、下拉框、下拉多选框设值及返回值的处理</title>
<script type="text/javascript" src="jquery-1.11.1.js"></script>
<script type="text/javascript">
$(function (){
$(":text").blur(function (){
var content = $("#address").val()
content = $.trim(content);
if(content == ""){
this.value = this.defaultValue; //defaultValue:是DOM中,text的默认属性值
}
});
//使单选下拉框的选择3号被选中
$(":button:eq(1)").click(function (){
$("#sigal").val("选择3号");
});
//使多选下拉框选中的选择2号和选择4号被选中
$(":button:eq(2)").click(function (){
$("#multiple").val(["选择2号","选择4号"]);
});
//使多选框的多选2和多选4被选中
$(":button:eq(3)").click(function (){
$(":checkbox[name='c']").val(["check2","check4"]);
});
//使单选框的单选2被选中
$(":button:eq(4)").click(function (){
$(":radio[name='r']").val(["radio2"]);
}); $(":button:eq(5)").click(function (){
alert($("#sigal").val());
alert($("#multiple").val());
//注意:此处有多个值时需要对其进行循环遍历,否则只能输出被选中的第一个值
$(":checkbox[name='c']:checked").each(function (){
alert($(this).val());
});
//radio可以不用遍历直接输出,因为被选中的只能有一个
//而且,在选取元素的时候不要忘记加 :checked,表示备选中的
alert($(":radio[name='r']:checked").val());
}); });
</script>
</head>
<body>
<input type="text" id="address" placeholder="请输入邮箱地址" /><br/>
<input type="text" name="password" placeholder="请输入邮箱密码" /><br/>
<input type="button" value="登录" /><br/><br/><br/> <input type="button" value="使单选下拉框的选择3号被选中" />
<input type="button" value="使多选下拉框选中的选择2号和选择4号被选中" /><br/>
<input type="button" value="使多选框的多选2和多选4被选中" />
<input type="button" value="使单选框的单选2被选中" /><br/>
<input type="button" value="打印已经被选中的值" /><br/><br/> <select id="sigal">
<option>选择1号</option>
<option>选择2号</option>
<option>选择3号</option>
</select> <select id="multiple" multiple="multiple">
<option>选择1号</option>
<option>选择2号</option>
<option>选择3号</option>
<option>选择4号</option>
</select><br/><br/> <input type="checkbox" name="c" value="check1" />多选1
<input type="checkbox" name="c" value="check2" />多选2
<input type="checkbox" name="c" value="check3" />多选3
<input type="checkbox" name="c" value="check4" />多选4<br/><br/> <input type="radio" name="r" value="radio1" />单选1
<input type="radio" name="r" value="radio2" />单选2
<input type="radio" name="r" value="radio3" />单选3
</body>
</html>
使用jQuery为文本框、单选框、多选框、下拉框、下拉多选框设值及返回值的处理的更多相关文章
- jQuery获取及设置单选框、多选框、文本框内容
获取一组radio被选中项的值 var item = $('input[@name=items][@checked]').val(); 获取select被选中项的文本 var item = $(&qu ...
- jQuery获取及设置单选框、多选框、文本框
获取一组radio被选中项的值 var item = $("input[@name=items][@checked]").val(); 获取select被选中项的文本 var it ...
- js/jquery获取文本框的值与改变文本框的值
我们就用它来学习获取文本框的值及改变文本框的值. 代码如下 复制代码 <script>function get1(){ document.getElementById("txtb ...
- jquery获取文本框的内容
使用jquery获取文本框的内容有以下几种: 1.根据ID取值(id属性): // javascript <script type="text/javascript"> ...
- jQuery清除文本框,内容并设置不可用
JQuery清除文本框,内容并设置不可用 如果是设置只读,则将disabled换成readonly function CleanText(textid) { $("#"+text ...
- Jquery对文本框的值、字符串的验证;正则表达式字符串的验证
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- 用Jquery控制文本框只能输入数字和字母
用Jquery控制文本框只能输入数字和字母 $.fn.onlyNum = function () { $(this).keypress(function (event) { var eventObj ...
- jQuery之文本框得失焦点
版本一 css代码部分: .focus { border: 1px solid #f00; background: #fcc; } 当焦点获得时,添加focus样式,添加边框,并改背景色为#fcc h ...
- jquery 实现文本框scroll上下动
<html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="C ...
随机推荐
- 001jsp的基本知识-包括生命周期,怎么编译等等
4 Jsp基础 4.1 Jsp引入 Servlet的作用: 用java语言开发动态资源的技术!!! Jsp的作用:用java语言(+html语言)开发动态资源的技术!!! Jsp就是servlet!! ...
- 断言(assert)和程序的安全保证
断言,用来DEBUG错误的,在DEBUG时发现然后跟踪错误! 通常 写一个程序给别人使用的,这个代码在安全性上的要求是什么呢?直觉上,我们都知道程序不应该崩.但是通常C/C++的程序如果把包含API的 ...
- c# http请求添加cookie
CookieCollection cookList = new CookieCollection(); cookList.Add(new Cookie("cf_clearance" ...
- springcloud微服务架构搭建
SpringCloud微服务框架搭建 一.微服务架构 1.1什么是分布式 不同模块部署在不同服务器上 作用:分布式解决网站高并发带来问题 1.2什么是集群 多台服务器部署相同应用构成一个集群 作用:通 ...
- WPF datagrid 弹出右键菜单时先选中该项
private void datagrid_PreviewMouseRightButtonDown(object sender, MouseButtonEventArgs e) { ...
- ReactNative iOS源码解析
http://awhisper.github.io/2016/06/24/ReactNative%E6%B5%81%E7%A8%8B%E6%BA%90%E7%A0%81%E5%88%86%E6%9E% ...
- python 发送email
pyton smtplib发送邮件 在邮件中设置并获取到 smtp域名 在脚本中执行命名,收件人可以是 多个,在列表中 import smtplib from email.mime.text impo ...
- php 快速读取文件夹下文件列表
在读取某个文件夹下的内容的时候 以前是使用 opendir readdir结合while循环过滤 . ..当前文件夹和父文件夹来操作的. 代码如下: 然后偶然发现了有scandir函数 可以扫描文件夹 ...
- Axure 8 注册码,市面上很多注册码都不行用,但是这个可以。
找了很久了,感谢@Quan-Sunny的转载 Licensee: University of Science and Technology of China (CLASSROOM) Key: DTXR ...
- git练习
git commit 提交记录 git branch <branch_name> 建立名为branch_name的分支 git checkout <name>:git comm ...