首页 > php教程 > PHP开发 > 正文

jQuery 下拉菜单操作总结

高洛峰
发布: 2016-12-15 16:16:38
原创
1418人浏览过

获取select :

 获取select 选中的 text :

   $("#ddlregtype").find("option:selected").text();

 

 获取select选中的 value:

   $("#ddlregtype ").val();

 

 获取select选中的索引:

     $("#ddlregtype ").get(0).selectedindex;

 

设置select:

 设置select 选中的索引:

     $("#ddlregtype ").get(0).selectedindex=index;//index为索引值

 

 设置select 选中的value:

    $("#ddlregtype ").attr("value","normal“);

    $("#ddlregtype ").val("normal");

    $("#ddlregtype ").get(0).value = value;

 

 设置select 选中的text:

var count=$("#ddlregtype option").length;

  for(var i=0;i<count;i++)  
     {           if($("#ddlregtype ").get(0).options[i].text == text)  
        {  
            $("#ddlregtype ").get(0).options[i].selected = true;  
          
            break;  
        }  
    }

 

$("#select_id option[text='jquery']").attr("selected", true);

 

设置select option项:

 

 $("#select_id").append("<option value='value'>text</option>");  //添加一项option

 $("#select_id").prepend("<option value='0'>请选择</option>"); //在前面插入一项option

 $("#select_id option:last").remove(); //删除索引值最大的option

 $("#select_id option[index='0']").remove();//删除索引值为0的option

 $("#select_id option[value='3']").remove(); //删除值为3的option

 $("#select_id option[text='4']").remove(); //删除text值为4的option

 

清空 select:

$("#ddlregtype ").empty();


jquery获得值:

.val()

.text()


设置值 

.val('在这里设置值')



$("document").ready(function(){ 
$("#btn1").click(function(){ 
$("[name='checkbox']").attr("checked",'true');//全选 
}) 
$("#btn2").click(function(){ 
$("[name='checkbox']").removeattr("checked");//取消全选 
}) 
$("#btn3").click(function(){ 
$("[name='checkbox']:even").attr("checked",'true');//选中所有奇数 
}) 
$("#btn4").click(function(){ 
$("[name='checkbox']").each(function(){//反选 
if($(this).attr("checked")){ 
$(this).removeattr("checked"); 

else{ 
$(this).attr("checked",'true'); 

}) 
}) 
$("#btn5").click(function(){//输出选中的值 
var str=""; 
$("[name='checkbox'][checked]").each(function(){ 
str+=$(this).val()+"\r\n"; 
//alert($(this).val()); 
}) 
alert(str); 
}) 
}) 

==================================================================

==================================================================

jQuery.fn.size = function()     
{     
    return jQuery(this).get(0).options.length;     
}     
//获得选中项的索引     
jQuery.fn.getSelectedIndex = function()     
{     
    return jQuery(this).get(0).selectedIndex;     
}     
//获得当前选中项的文本     
jQuery.fn.getSelectedText = function()     
{     
    if(this.size() == 0)     
    {     
        return \"下拉框中无选项\";     
    }     
    else    
    {     
        var index = this.getSelectedIndex();           
        return jQuery(this).get(0).options[index].text;     
    }     
}     
//获得当前选中项的值     
jQuery.fn.getSelectedValue = function()     
{         
    if(this.size() == 0)     
    {     
        return \"下拉框中无选中值\";     
    }     
    else    
    {     
        return jQuery(this).val();     
    }     
}     
//设置select中值为value的项为选中     
jQuery.fn.setSelectedValue = function(value)     
{     
    jQuery(this).get(0).value = value;     
}     
//设置select中文本为text的第一项被选中     
jQuery.fn.setSelectedText = function(text)     
{     
    var isExist = false;     
    var count = this.size();     
    for(var i=0;i<count;i++)     
    {     
        if(jQuery(this).get(0).options[i].text == text)     
        {     
            jQuery(this).get(0).options[i].selected = true;     
            isExist = true;     
            break;     
        }     
    }     
    if(!isExist)     
    {     
        alert(\"下拉框中不存在该项\");     
    }     
}     
//设置选中指定索引项     
jQuery.fn.setSelectedIndex = function(index)     
{     
    var count = this.size();         
    if(index >= count || index < 0)     
    {     
        alert(\"选中项索引超出范围\");     
    }     
    else    
    {     
        jQuery(this).get(0).selectedIndex = index;     
    }     
}     
//判断select项中是否存在值为value的项     
jQuery.fn.isExistItem = function(value)     
{     
    var isExist = false;     
    var count = this.size();     
    for(var i=0;i<count;i++)     
    {     
        if(jQuery(this).get(0).options[i].value == value)     
        {     
            isExist = true;     
            break;     
        }     
    }     
    return isExist;     
}     
//向select中添加一项,显示内容为text,值为value,如果该项值已存在,则提示     
jQuery.fn.addOption = function(text,value)     
{     
    if(this.isExistItem(value))     
    {     
        alert(\"待添加项的值已存在\");     
    }     
    else    
    {     
        jQuery(this).get(0).options.add(new Option(text,value));     
    }     
}     
//删除select中值为value的项,如果该项不存在,则提示     
jQuery.fn.removeItem = function(value)     
{         
    if(this.isExistItem(value))     
    {     
        var count = this.size();             
        for(var i=0;i<count;i++)     
        {     
            if(jQuery(this).get(0).options[i].value == value)     
            {     
                jQuery(this).get(0).remove(i);     
                break;     
            }     
        }             
    }     
    else    
    {     
        alert(\"待删除的项不存在!\");     
    }     
}     
//删除select中指定索引的项     
jQuery.fn.removeIndex = function(index)     
{     
    var count = this.size();     
    if(index >= count || index < 0)     
    {     
        alert(\"待删除项索引超出范围\");     
    }     
    else    
    {     
        jQuery(this).get(0).remove(index);     
    }     
}     
//删除select中选定的项     
jQuery.fn.removeSelected = function()     
{     
    var index = this.getSelectedIndex();     
    this.removeIndex(index);     
}     
//清除select中的所有项     
jQuery.fn.clearAll = function()     
{     
    jQuery(this).get(0).options.length = 0;     
}   

BibiGPT-哔哔终结者
BibiGPT-哔哔终结者

B站视频总结器-一键总结 音视频内容

BibiGPT-哔哔终结者 28
查看详情 BibiGPT-哔哔终结者

更多 jQuery 下拉菜单操作总结相关文章请关注PHP中文网!

最佳 Windows 性能的顶级免费优化软件
最佳 Windows 性能的顶级免费优化软件

每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。

下载
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号