javascript代码实例教程-jQuery操作SelectOption上下移动

发布时间:2019-02-21 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了javascript代码实例教程-jQuery操作SelectOption上下移动脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。
小宝典致力于为广大程序猿(媛)提供高品质的代码服务,请大家多多光顾小站,小宝典在此谢过。 <;meta http-equiv="content-tyPE" content="text/htML; charset=UTF-8"> &nbsp;

  

<script type="text/javascript" src="jquery-1.9.1.min.js"></script>  

  

<script type="text/javascript">  

/** 

 * 向上移动选中的option 

 */  

function upSelectedOption(){  

    if(null == $(&#39;#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>  

  

<p 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>  

</p>  

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

觉得可用,就经常来吧! 脚本宝典 欢迎评论哦! js脚本,巧夺天工,精雕玉琢。小宝典献丑了!

脚本宝典总结

以上是脚本宝典为你收集整理的javascript代码实例教程-jQuery操作SelectOption上下移动全部内容,希望文章能够帮你解决javascript代码实例教程-jQuery操作SelectOption上下移动所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。