使用val()方法设置表单中的默认选中项
有时候我们展示给用户的表单中的checkbox,radio,selec等标签的一些项是默认选中的。比方:当用户改动文章的时候,假设相应的栏目为下拉框的话,那么它的默认选中值应该是原来的栏目位置。
能够使用jquery中的val()方法给select、checkbox、radio设置默认选中项。
对于multiple类型的select和checkbox还能够设置多个默认值。
效果图:
方法:
$("select#multiple").val(["选择2号","选择4号"]);
完整代码:
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>jquery test</title>
<script src="jquery-1.11.1.min.js"></script>
<style type="text/css">
div
{
margin-top:50px;
margin-left:100px;
}
</style>
</head> <body>
<div>
<select id="single">
<option value="选择1号">选择1号</option>
<option value="选择2号">选择2号</option>
<option value="选择3号">选择3号</option>
<option value="选择4号">选择4号</option>
</select>
</div>
<div>
<select id="multiple" multiple="multiple" style="height:120px;">
<option value="选择1号">选择1号</option>
<option value="选择2号">选择2号</option>
<option value="选择3号">选择3号</option>
<option value="选择4号">选择4号</option>
</select>
</div>
<div>
<input type="checkbox" value="check1"/>多选1
<input type="checkbox" value="check2"/>多选2
<input type="checkbox" value="check3"/>多选3
<input type="checkbox" value="check4"/>多选4
</div>
<div>
<input type="radio" value="radio1">单选1
<input type="radio" value="radio2">单选2
<input type="radio" value="radio3">单选3
<input type="radio" value="radio4">单选4
</div>
</body>
<script type="text/javascript">
$("select#single").val(["选择4号"]);
$("select#multiple").val(["选择2号","选择4号"]);
$(":checkbox").val(["check1","check3"]);
$(":radio").val(["radio4"]);
</script>
</html>
使用val()方法设置表单中的默认选中项的更多相关文章
- angularjs ng-select ng-options 默认选中项.
<!DOCTYPE html> <html ng-app="myApp"> <head> <meta charset="utf- ...
- Android RadioGroup设置默认选中项
今天有人问.Android 里面 RadioGroup里面有两个RadioButton怎么设置默认值? 第一个RadioButton设置 android:checked="true" ...
- php处理表单中的复选框问题以及js实现全选
做的一个项目中遇到了全选和取消全选的问题,这是一个很普遍的功能,,虽然我们经常用到,但是真正做起来却发现行不通,在网上找了些,大部分都是ie,但是谷歌内核浏览器不能正常实现,所以经过小小的调整,今天就 ...
- Jquery 根据value值设置下拉列表(select)默认选中项
方法一: $("#selIndustyType option[value='1']").attr("selected", "selected" ...
- jstree前端设置默认选中项
$("#jstree").on("loaded.jstree", function (event, data) { var currDeptId = crm.g ...
- laravel中select2多选,初始化默认选中项
项目中有发送消息功能,需要能通过搜索,多选用户,来指定发送人.使用 select2 插件来完成. select2 的 html 代码如下: <div class="form-group ...
- @Html.DropDownListFor默认选中项
http://q.cnblogs.com/q/73902/ 项目使用mvc4,给dropDownList指定默认值未选中 页面代码是: 1.未有默认选中值 Html.DropDownListFor(m ...
- html <select> 用JS控制默认选中项
<html> <head> <!--禁止页面缓存--><meta http-equiv="content-type" content=&q ...
- easyui combobox默认选中项
今天写前端代码发现combobox还挺难搞, $("#select_Dic").combobox({ url: "http: ...
随机推荐
- C语言-02基本运算
1.除法运算 / 整数除于整数,还是整数.参与运算的操作数必须是同类型. 1/2的值是0.参与运算的操作数是什么类型,得出的值就是什么类型. 10.5 + 10将右边的10提升为了double类型 ...
- BZOJ3190[JLOI2013]赛车
Description 这里有一辆赛车比赛正在进行,赛场上一共有N辆车,分别称为个g1,g2--gn.赛道是一条无限长的直线.最初,gi位于距离起跑线前进ki的位置.比赛开始后,车辆gi将会以vi单位 ...
- Android中JNI编程的那些事儿(1)
转:Android中JNI编程的那些事儿(1)http://mobile.51cto.com/android-267538.htm Android系统不允许一个纯粹使用C/C++的程序出现,它要求必须 ...
- Codeforces Round #197 (Div. 2) : D
这题也是一个线段树的水题: 不过开始题目没看明白,害得我敲了一个好复杂的程序.蛋疼啊.... 最后十几分钟的时候突然领悟到了题意,但是还是漏掉一个细节,老是过不去... 以后比赛的时候不喝啤酒了,再也 ...
- [转贴]PHP 开发者应了解的 24 个库
作为一个PHP开发者,现在是一个令人激动的时刻.每天有许许多多有用的库分发出来,在Github上很容易发现和使用这些库.下面是我曾经遇到过最酷的24个库.你最喜欢的库没有在这个列表里面?那就在评论中分 ...
- 【Xamarin挖墙脚系列:时刻下载最新的Mac环境下的Xamarin安装包】
原文:[Xamarin挖墙脚系列:时刻下载最新的Mac环境下的Xamarin安装包] 打开这两个地址,就能看到最新的安装包了.... http://www.jianshu.com/p/c67c14b3 ...
- Android Training精要(五)讀取Bitmap對象實際的尺寸和類型
讀取Bitmap對象實際的尺寸和類型 BitmapFactory.Options options = new BitmapFactory.Options(); options.inJustDecode ...
- AlertDialog.Builder 样式设置
AlertDialog.Builder builder = new AlertDialog.Builder(new ContextThemeWrapper(this, R.style.AlertDia ...
- 从C#到Python —— 4 类及面向对象
http://www.cnblogs.com/yanxy/archive/2010/04/04/c2p_4.html 如果你熟悉C#,那么对类(Class)和面向对象(Object Oriented) ...
- 嵌套滚动demo
https://github.com/luv135/NestedScrollingDemo https://github.com/ggajews/nestedscrollingchildviewdem ...