最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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()"/>

相關文章

最新評論

南漳县| 吴桥县| 临沧市| 石柱| 阿勒泰市| 厦门市| 呼和浩特市| 万年县| 晋州市| 彰化县| 绥宁县| 商城县| 台中市| 邢台市| 桃江县| 冷水江市| 新沂市| 藁城市| 山东省| 大竹县| 滕州市| 威信县| 三明市| 金沙县| 濮阳县| 辉南县| 汤阴县| 白玉县| 商南县| 鄯善县| 蕲春县| 彩票| 榆树市| 泰兴市| 台州市| 罗甸县| 镶黄旗| 黑山县| 滁州市| 册亨县| 桑植县|