JQuery 操作select

Posted aLa神灯

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了JQuery 操作select相关的知识,希望对你有一定的参考价值。

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<script type="text/javascript" src=‘jquery-1.7.2.js‘ ></script> 
<title>Insert title here</title>
<script type="text/javascript">

    $(function() {
        
        //获取选中的值   value--值  text--文本
        //1.
        /* $("select :selected").each(function() {
            //alert(this.value);
            alert(this.text);
        });  */ 
        //2.
        //alert($("#se").val());
        //alert($("#se").text());
        //3.
        //alert($("#se").find("option:selected").text());
        //alert($("#se").find("option:selected").val());
        //4.
        //alert($("#se").get(0).value);
        
        //获取索引值
        //alert($("#se").get(0).selectedIndex);
        //alert($("#se option:first").attr("index"));
        
        //alert($("#se option:first").val());
        
        
        //添加
        //$("#se").append("<option value=‘1‘>11</option>");
          // $("#se").prepend("<option value=‘1‘>11</option>");
           
            
        //内容清空
        //$("#se").empty();
        
        //删除
        //$("#se option:last ").remove();
        //$("#se option:[value = ‘3‘] ").remove(); // 删除value 值为3
        //$("#se").remove();
        
        //设置被选中
        //$("#se option:[value = ‘3‘] ").attr("selected",true);
        
        // 默认值改变第一个
        //alert($(" option:first").text(0));
    });
</script>
</head>
<body>
<select id=‘se‘>
<option value=‘2‘>22</option>
<option value=‘3‘>33</option>
</select>

<select id=‘s2‘>
<option value=‘2‘>22</option>
<option value=‘3‘>33</option>
</select>
</body>
</html>

 

以上是关于JQuery 操作select的主要内容,如果未能解决你的问题,请参考以下文章

jquery基本操作

jquery操作select取值赋值与设置选中

jQuery select操作控制方法小结

jQuery应用 代码片段

Jquery操作下拉框(DropDownList)实现取值赋值

Visual Studio 2012-2019的130多个jQuery代码片段。