jQuery操作Select的Option上下移動(dòng)及移除添加等等
<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">
/**
* 向上移動(dòng)選中的option
*/
function upSelectedOption(){
if(null == $('#where').val()){
alert('請(qǐng)選擇一項(xiàng)');
return false;
}
//選中的索引,從0開(kāi)始
var optionIndex = $('#where').get(0).selectedIndex;
//如果選中的不在最上面,表示可以移動(dòng)
if(optionIndex > 0){
$('#where option:selected').insertBefore($('#where option:selected').prev('option'));
}
}
/**
* 向下移動(dòng)選中的option
*/
function downSelectedOption(){
if(null == $('#where').val()){
alert('請(qǐng)選擇一項(xiàng)');
return false;
}
//索引的長(zhǎng)度,從1開(kāi)始
var optionLength = $('#where')[0].options.length;
//選中的索引,從0開(kāi)始
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('請(qǐng)選擇一項(xiàng)');
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('當(dāng)前被選中的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('當(dāng)前被選中的編號(hào)順序?yàn)? + ids);
}
/**
* 添加option
*/
function addSelectedOption(){
//添加在第一個(gè)位置
$('#where').prepend('<option value="hbin" id="where06">Haerbin</option>');
//添加在最后一個(gè)位置
$('#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詳解(取值,設(shè)置選中)
- jquery實(shí)現(xiàn)動(dòng)態(tài)操作select選中
- jQuery操作select下拉框的text值和value值的方法
- jquery操作select option 的代碼小結(jié)
- 利用jquery操作select下拉列表框的代碼
- jQuery操作Select選擇的Text和Value(獲取/設(shè)置/添加/刪除)
- jquery操作select大全
- jQuery操作select的實(shí)例代碼
- jquery操作select元素和option的實(shí)例代碼
- jquery下拉select控件操作方法分享(jquery操作select)
- jquery操作select常見(jiàn)方法大全【7種情況】
相關(guān)文章
一個(gè)簡(jiǎn)單的jquery的多選下拉框(自寫(xiě))
想在網(wǎng)上找一個(gè)插件用用,可是,網(wǎng)上的插件看起來(lái)都比較雜亂,我參考了網(wǎng)上的一些插件,自己用jquery寫(xiě)了一個(gè)多選下拉框2014-05-05用jquery中插件dialog實(shí)現(xiàn)彈框效果實(shí)例代碼
這篇文章介紹了jquery中插件dialog實(shí)現(xiàn)彈框效果實(shí)例代碼,有需要的朋友可以參考一下2013-11-11省市區(qū)三級(jí)聯(lián)動(dòng)jquery實(shí)現(xiàn)代碼
這篇文章主要為大家詳細(xì)紹了省市區(qū)三級(jí)聯(lián)動(dòng)jquery實(shí)現(xiàn)代碼,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-10-10jQuery使用一個(gè)按鈕控制圖片的伸縮實(shí)現(xiàn)思路
上一篇講解了jquery同時(shí)控制四張圖片的伸縮,想必大家對(duì)伸縮有一定了解了,本文將為大家介紹下使用按鈕控制圖片的伸縮,感興趣的朋友可以參考下哈2013-04-04jQuery實(shí)現(xiàn)導(dǎo)航高亮的方法【附demo源碼下載】
這篇文章主要介紹了jQuery實(shí)現(xiàn)導(dǎo)航高亮的方法,涉及針對(duì)鼠標(biāo)事件的相應(yīng)及頁(yè)面元素屬性動(dòng)態(tài)變換的相關(guān)操作技巧,并附帶demo源碼供讀者下載,需要的朋友可以參考下2016-11-11分享十五款 jQuery 社交網(wǎng)絡(luò)分享插件
這篇文章主要介紹了分享十五款 jQuery 社交網(wǎng)絡(luò)分享插件,需要的朋友可以參考下2015-05-05jquery實(shí)現(xiàn)浮動(dòng)的側(cè)欄實(shí)例
這篇文章主要介紹了jquery實(shí)現(xiàn)浮動(dòng)的側(cè)欄,實(shí)例分析了基于jQuery的stickySidebar插件實(shí)現(xiàn)浮動(dòng)層的相關(guān)技巧,需要的朋友可以參考下2015-06-06jquery實(shí)現(xiàn)表格行的上下移動(dòng)和置頂
這篇文章主要為大家詳細(xì)介紹了jquery實(shí)現(xiàn)表格行的上下移動(dòng)和置頂,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-02-02