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>

猜你喜欢

转载自www.cnblogs.com/lxh520/p/9078312.html