国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > JavaScript > 正文

jQuery操作Select的Option上下移動及移除添加等等

2019-11-20 21:40:13
字體:
來源:轉載
供稿:網友
復制代碼 代碼如下:

<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()"/>
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 乌兰察布市| 黄陵县| 翁源县| 铜鼓县| 黎平县| 涞水县| 祁东县| 舒城县| 比如县| 南皮县| 卢氏县| 得荣县| 博乐市| 荃湾区| 绥中县| 清新县| 会泽县| 四会市| 嵊泗县| 靖宇县| 甘肃省| 通许县| 平武县| 临朐县| 永川市| 于田县| 寻乌县| 灵武市| 西华县| 南华县| 策勒县| 青铜峡市| 临夏县| 碌曲县| 老河口市| 黔西县| 乌鲁木齐市| 石楼县| 夏津县| 丽江市| 灯塔市|