jquery 对select中option的移动删除
来源:互联网 发布:2015人口普查数据 编辑:程序博客网 时间:2024/05/24 20:08
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<script type="text/javascript" src="jquery-2.1.0.js"></script>
<script type="text/javascript">
/**
* 向上移动选中的option
*/
function upSelectedOption(){
if(null == $('#where').val()){
alert('请选择一项');
return false;
}
//选中的索引,从0开始
var optionIndex = $('#where').get(0).selectedIndex; // 属性可设置或返回下拉列表中被选选项的索引号
//如果选中的不在最上面,表示可以移动
if(optionIndex > 0){
$('#where option:selected').insertBefore($('#where option:selected').prev('option'));
}
}
/**
* 向下移动选中的option
*/
function downSelectedOption(){
if(null == $('#where').val()){
alert('请选择一项');
return false;
}
//索引的长度,从1开始
var optionLength = $('#where')[0].options.length;
//选中的索引,从0开始
var optionIndex = $('#where').get(0).selectedIndex;
//如果选择的不在最下面,表示可以向下
if(optionIndex < (optionLength-1)){
$('#where option:selected').insertAfter($('#where option:selected').next('option'));
}
}
/**
* 移除选中的option
*/
function removeSelectedOption(){
if(null == $('#where option:selected').val()){
alert('请选择一项');
return false;
}
$('#where option:selected').remove();
}
/**
* 获取所有的option值
*/
function getSelectedOption(){
//获取Select选择的Text
//var checkText = $('#where').find('option:selected').text();
//获取Select选择的Value
var checkValue = $('#where').find('option:selected').val();
alert('当前被选中的text=' + checkText + ', value=' + checkValue);
var ids = '';
var options = $('#where')[0].options;
for(var i=0; i<options.length; i++){
ids = ids + '`' + options[i].id;
}
alert('当前被选中的编号顺序为' + ids);
}
/**
* 添加option
*/
function addSelectedOption(){
//添加在第一个位置
$('#where').prepend('<option value="hbin" id="where06">Haerbin</option>');
//添加在最后一个位置
$('#where').append('<option value="hlj" id="where07">HeiLongJiang</option>');
$('#where').attr('size', 7);
}
</script>
<div id="updown">
<select id="where" name="where" size="5">
<option value="hk" id="where01">Hong Kong</option>
<option value="tw" id="where02">Taiwan</option>
<option value="cn" id="where03">China</option>
<option value="us" id="where04">United States</option>
<option value="ca" id="where05">Canada</option>
</select>
</div>
<br/>
<input type="button" value="上移" onclick="upSelectedOption()"/>
<input type="button" value="下移" onclick="downSelectedOption()"/>
<input type="button" value="删除" onclick="removeSelectedOption()"/>
<input type="button" value="确定" onclick="getSelectedOption()"/>
<input type="button" value="添加" onclick="addSelectedOption()"/>
<script type="text/javascript" src="jquery-2.1.0.js"></script>
<script type="text/javascript">
/**
* 向上移动选中的option
*/
function upSelectedOption(){
if(null == $('#where').val()){
alert('请选择一项');
return false;
}
//选中的索引,从0开始
var optionIndex = $('#where').get(0).selectedIndex; // 属性可设置或返回下拉列表中被选选项的索引号
//如果选中的不在最上面,表示可以移动
if(optionIndex > 0){
$('#where option:selected').insertBefore($('#where option:selected').prev('option'));
}
}
/**
* 向下移动选中的option
*/
function downSelectedOption(){
if(null == $('#where').val()){
alert('请选择一项');
return false;
}
//索引的长度,从1开始
var optionLength = $('#where')[0].options.length;
//选中的索引,从0开始
var optionIndex = $('#where').get(0).selectedIndex;
//如果选择的不在最下面,表示可以向下
if(optionIndex < (optionLength-1)){
$('#where option:selected').insertAfter($('#where option:selected').next('option'));
}
}
/**
* 移除选中的option
*/
function removeSelectedOption(){
if(null == $('#where option:selected').val()){
alert('请选择一项');
return false;
}
$('#where option:selected').remove();
}
/**
* 获取所有的option值
*/
function getSelectedOption(){
//获取Select选择的Text
//var checkText = $('#where').find('option:selected').text();
//获取Select选择的Value
var checkValue = $('#where').find('option:selected').val();
alert('当前被选中的text=' + checkText + ', value=' + checkValue);
var ids = '';
var options = $('#where')[0].options;
for(var i=0; i<options.length; i++){
ids = ids + '`' + options[i].id;
}
alert('当前被选中的编号顺序为' + ids);
}
/**
* 添加option
*/
function addSelectedOption(){
//添加在第一个位置
$('#where').prepend('<option value="hbin" id="where06">Haerbin</option>');
//添加在最后一个位置
$('#where').append('<option value="hlj" id="where07">HeiLongJiang</option>');
$('#where').attr('size', 7);
}
</script>
<div id="updown">
<select id="where" name="where" size="5">
<option value="hk" id="where01">Hong Kong</option>
<option value="tw" id="where02">Taiwan</option>
<option value="cn" id="where03">China</option>
<option value="us" id="where04">United States</option>
<option value="ca" id="where05">Canada</option>
</select>
</div>
<br/>
<input type="button" value="上移" onclick="upSelectedOption()"/>
<input type="button" value="下移" onclick="downSelectedOption()"/>
<input type="button" value="删除" onclick="removeSelectedOption()"/>
<input type="button" value="确定" onclick="getSelectedOption()"/>
<input type="button" value="添加" onclick="addSelectedOption()"/>
阅读全文
0 0
- jquery 对select中option的移动删除
- jquery 对select中option的操作
- JQuery中对Select的option项的添加、删除、取值(带输入框)
- JQuery中对Select的option项的添加、删除、取值
- JQuery中对Select的option项的添加、删除、取值
- JQuery中对Select的option项的添加、删除、取值
- jquery删除select中所有option选项
- JQuery Select 删除Option
- jQuery添加/删除Select的Option项
- JQuery 对 Select option 的操作
- Jquery 对select option 的操作
- JQuery 对 Select option 的操作
- JQuery 对 Select option 的操作
- JQuery 对 Select option 的操作
- Jquery对select 和option 的使用
- JQuery 对 Select option 的操作
- JQuery 对 Select option 的操作
- JQuery 对 Select option 的操作
- 电商业务之代码逻辑(订单售后)
- CodeForces788C【BFS】
- TortoiseSVN 设置中文语言包
- 数据结构之单链表
- 170520 cmd 下带参数执行python 文件
- jquery 对select中option的移动删除
- 求无向图的 割点和桥 【模版】
- 1116. Come on! Let's C (20)[map]
- 树莓派 驱动步进电机 python版
- JAVA反射机制
- 双向队列元素的插入与删除
- 数据中聚类个数的确定(Determining the number of clusters in a data set)
- 用树莓派做魔镜
- Git 日常使用的命令