Jquery对select的操作(附日历天数变化代码)
转载请注明出处。
逃不开传统的四种操作:增、删、改、查。
<四处搜刮了jquery对select操作的代码,汇集一下,方便以后查看。日历天数变化代码为原创。>
[增]:
$("#select_id").append("<option value='Value'>Text</option>"); //为Select追加一个Option(下拉项)
$("#select_id").prepend("<option value='0'>请选择</option>"); //为Select插入一个Option(第一个位置)
[删]:
$("#select_id option:last").remove(); //删除Select中索引值最大Option(最后一个)
$("#select_id option[index='0']").remove(); //删除Select中索引值为0的Option(第一个)
$("#select_id option[value='3']").remove(); //删除Select中Value='3'的Option
$("#select_id option[text='4']").remove(); //删除Select中Text='4'的Option
$("#select_id").empty(); //清空
[改](设置选中项):
$("#select_id ").get(0).selectedIndex=1; //设置Select索引值为1的项选中
$("#select_id ").val(4); //设置Select的Value值为4的项选中
$("#select_id option[text='jQuery']").attr("selected", true); //设置Select的Text值为jQuery的项选中
[查](获取选中值):
1.获取选中项的value
$("#select_id").val(); //获取选中项的value
$("#select_id ").get(0).selectedIndex; //获取选中项的索引值
$("#select_id").find("option:selected").text(); //获取选中项的text
$("#select_id option:last").attr("index"); //获取Select最大的索引值
附上代码样例,代码功能为根据实际选择的“年”、“月”,来改变select“日”中option的天数。
使用php编写,默认$("select.day")初始有31天,因为默认为1月:
<select name ="date_year" class="year"> //年
<?php
for ($year = 1990; $year <= date("Y"); ++$year) {
?>
<option value="<?php echo $year;?>"><?php echo $year;?></option>
<?php
}
?>
</select>
<select name ="date_month" class="month"> //月
<?php
for ($month = 1; $month <= 12; ++$month) {
?>
<option value="<?php echo $month;?>"><?php echo $month;?></option>
<?php
}
?>
</select>
<select name ="date_day" class="day"> //日
<?php
for ($day = 1; $day <= 31; ++$day) {
?>
<option value="<?php echo $day;?>"><?php echo $day;?></option>
<?php
}
?>
</select>
jquery代码:
$(document).ready(function() {
$("select.month, select.year").change(function() { //"年"、"月"的变化都可能引起“日”的变化
$("select.day").empty(); //非常重要,要先清空
for (var i = 1; i < 31; i++) {
var option = $("<option>").val(i).text(i);
$("select.day").append(option);
}
var month = $("select.month").val();
if ((month % 2 && month < 8) || (month % 2 === 0 && month > 7)) {
$("select.day").append("<option value='31'>31</option>"); //天数为31天的append一个option
}
if (month === 2) {
$("select.day option:last").remove();
$("select.day option:last").remove(); //2月天数28
var year = $("select.year").val();
if ((year % 4 === 0 && year % 100) || year % 400 === 0)
$("select.day").append("<option value='29'>29</option>"); //闰年2月天数加一
}
});
});
Jquery对select的操作(附日历天数变化代码)的更多相关文章
- 【jq】JQuery对select的操作
下拉框 <select id="selectID" name="selectName"> <option vlaue="1" ...
- jQuery关于Select的操作
jQuery获取Select选择的Text和Value: 1. var checkText=jQuery("#select_id").find("option:selec ...
- jQuery的select相关操作
例: <select class="selector" id="selector"></select> 1.设置value为pxx的项选 ...
- jquery的select 2库,如果用js代码刷新选项?
遇到的需求,select的显示为select 2的样式. 那如果用户全选时,如何能让select 2的样式更新呢? 1,引入select 2 $(".select2_single" ...
- jQuery select的操作代码
jQuery對select的操作的实际应用代码. //改變時的事件 复制代码代码如下: $("#testSelect").change(function(){ //事件發生 j ...
- jQuery对Select操作大集合
介绍了jQuery对Select的操作进行了详细的汇总. 1.jQuery添加/删除Select的Option项: 2.$("#select_id").append("& ...
- JQuery 对 Select option 的操作---转载
<select id="selectID" > <option value="1">1</option> <optio ...
- jquery获得select option的值和对select option的操作
<body> <select name="month" id="selMonth" onchange="set()"> ...
- jquery select 常用操作总结
由于在项目各种所需,经常碰到select不种操作的要求,今天特意总结了一下,分享: jQuery获取Select选择的Text和Value: 语法解释: 1. $("#select_id&q ...
随机推荐
- Spring AOP AspectJ Pointcut 表达式例子
主要来源:http://howtodoinjava.com/spring/spring-aop/writing-spring-aop-aspectj-pointcut-expressions-with ...
- javascript中的继承方法
从Javascript面向对象编程(二):构造函数的继承这里,可以看到详细的说明. 我只是将其中的例子做成html文件,便于调试罢了. 1. 构造函数绑定 <html> <head& ...
- 城市平乱(Bellman)
城市平乱 时间限制:1000 ms | 内存限制:65535 KB 难度:4 描述 南将军统领着N个部队,这N个部队分别驻扎在N个不同的城市. 他在用这N个部队维护着M个城市的治安,这M个城市 ...
- bootstrap 响应式布局 居中问题
大家能够通过table来设置居中: display: table; width: auto; margin-left: auto; margin-right: auto;
- Deep Learning(深度学习)学习笔记整理系列之(二)
Deep Learning(深度学习)学习笔记整理系列 zouxy09@qq.com http://blog.csdn.net/zouxy09 作者:Zouxy version 1.0 2013-04 ...
- Oracle expdp/impdp导出导入命令及数据库备份(转)
使用EXPDP和IMPDP时应该注意的事项: EXP和IMP是客户端工具程序,它们既可以在客户端使用,也可以在服务端使用. EXPDP和IMPDP是服务端的工具程序,他们只能在ORACLE服务端使用, ...
- SQL 语句优化—— (二) 索引的利用
索引是与表或视图关联的磁盘上结构,可以加快从表或视图中检索行的速度.索引包含由表或视图中的一列或多列生成的键.与书中的索引一样,数据库中的索引使您可以快速找到表或索引视图中的特定信息.索引包含从表或视 ...
- 通过git和Xcode将代码上传到GitHub
长话短说: 第一步:初始化一个本地仓库 git init 第二步:将你的文件添加到缓存区 git add . 后面的空格 . 是将全部的文件都添加到缓存区 可以使用git status 查看状态 ...
- BZOJ 1212: [HNOI2004]L语言( dp + trie )
因为单词很短...用trie然后每次dp暴力查找...用哈希+dp应该也是可以的.... ------------------------------------------------------- ...
- Struts学习之手动验证
* 首先要从页面中获取对应的标签name属性的值,在动作类action中声明同名的属性,提供get和set方法 * 要继承ActionSupport类或者实现Validateable接口 ...