实现option上下移动_jQuery操作Select的Option上下移动及移除添加等等

这篇博客介绍了如何使用JavaScript操作HTML Select元素,包括向上和向下移动选中的option,删除选中option,获取所有option的值,以及向Select中添加新选项。涉及到DOM操作和jQuery方法的应用。
摘要由CSDN通过智能技术生成

/**

* 向上移动选中的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').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').val();

alert('当前被选中的text=' + checkText + ', value=' + checkValue);

var ids = '';

var options = $('#where')[0].options;

for(var i=0; i

ids = ids + '`' + options[i].id;

}

alert('当前被选中的编号顺序为' + ids);

}

/**

* 添加option

*/

function addSelectedOption(){

//添加在第一个位置

$('#where').prepend('Haerbin');

//添加在最后一个位置

$('#where').append('HeiLongJiang');

$('#where').attr('size', 7);

}

Hong Kong

Taiwan

China

United States

Canada

Jquery取得iframe下内容的方法

document.all('Iframe1').contentWindow.document.getElementById("deptName").width;$(window.parent.Iframe1.document.body).find('#deptName').width();跨域调用IFRAME时会遇到拒绝访问.在网上找

Query中click(),bind(),live(),delegate()的区别

click(),bind(),live()都是执行事件时使用的方法,他们之前是有一些区别的,我们在使用这些方法时应该根据需要进行选择。1.click()方法是我们经常使用

jQuery控制iFrame(实例代码)

用jQuery在IFRAME里取得父窗口的某个元素的值只好用DOM方法与jquery方法结合的方式实现了1.在父窗口中操作选中IFRAME中的所有单选钮$(window.frames["iframe1"].doc

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值