jQuery操作Select的Option上下移動及移除添加等等
更新時間:2013年11月18日 15:52:15 作者:
jQuery操作Select Option:向上移動選中的option、向下移動選中的option、移除選中的option、獲取所有的option值、添加option等等,下面有個不錯的示例,感興趣的朋友不要錯過
復制代碼 代碼如下:
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<script type="text/javascript" src="jquery-1.9.1.min.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').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<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()"/>
您可能感興趣的文章:
- jquery操作select詳解(取值,設置選中)
- jquery實現(xiàn)動態(tài)操作select選中
- jQuery操作select下拉框的text值和value值的方法
- jquery操作select option 的代碼小結(jié)
- 利用jquery操作select下拉列表框的代碼
- jQuery操作Select選擇的Text和Value(獲取/設置/添加/刪除)
- jquery操作select大全
- jQuery操作select的實例代碼
- jquery操作select元素和option的實例代碼
- jquery下拉select控件操作方法分享(jquery操作select)
- jquery操作select常見方法大全【7種情況】
相關文章
用jquery中插件dialog實現(xiàn)彈框效果實例代碼
這篇文章介紹了jquery中插件dialog實現(xiàn)彈框效果實例代碼,有需要的朋友可以參考一下2013-11-11省市區(qū)三級聯(lián)動jquery實現(xiàn)代碼
這篇文章主要為大家詳細紹了省市區(qū)三級聯(lián)動jquery實現(xiàn)代碼,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2016-10-10jQuery實現(xiàn)導航高亮的方法【附demo源碼下載】
這篇文章主要介紹了jQuery實現(xiàn)導航高亮的方法,涉及針對鼠標事件的相應及頁面元素屬性動態(tài)變換的相關操作技巧,并附帶demo源碼供讀者下載,需要的朋友可以參考下2016-11-11