jQuery-select

瘦欲@ 提交于 2020-01-26 16:47:10
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
    
<title>jQuery-Select-Test</title>
    
<script src="http://media.pec365.com/rainnoless/javascript/jquery-1.3.2.min.js" type="text/javascript"></script>
    
<style type="text/css">
        .div
        
{
            width
:95%;
            margin-left
:15px; 
            margin-top
:15px; 
            margin-right
:15px; 
            padding-left
:5px;
            padding-top
:5px;
            padding-bottom
:5px;
            background-color
:#ccc; 
            border
:#999 1px solid;
            line-height
:30px;
            font-size
:13px;
            font-family
:微软雅黑;
        
}
        .blue
{color:Blue;}
        .green
{color:Green;}
        .button
{border:green 1px solid;width:230px;}
    
</style>
</head>
<body>
<div class="div">
jQuery获取Select选择的Text和Value:
<br />
<select id="slc1" >
    
<option value="0">请选择</option>
    
<option value="1">C#</option>
    
<option value="2">Javascript</option>
    
<option value="3">jQuery</option>
    
<option value="4">C++</option>
    
<option value="5">Java</option>
    
<option value="6">VB</option>
</select>&nbsp;
选择一项试试看
<br />
<script type="text/javascript">
    $(
"#slc1").change(function(){
        
var checkText=$("#slc1").find("option:selected").text();
        
var checkValue=$("#slc1").val();
        
var checkIndex=$("#slc1 ").get(0).selectedIndex;
        
var maxIndex=$("#slc1 option:last").attr("index");
        alert(
'您选择的Text是: '+checkText+"\r\n"+"您选择的Value是: "+checkValue+"\r\n您选择的索引值是: "+checkIndex+"\r\n最大索引值是: "+maxIndex);
    });
</script>
语法解释:
<br />
1. $("#select_id").change(function(){//code...}); 
&nbsp;&nbsp;<span class="green">//为Select添加事件,当选择其中一项时触发</span><br />
2. var checkText=$("#select_id").find("option:selected").text();
&nbsp;&nbsp;<span class="green">//获取Select选择的Text</span><br />
3. var checkValue=$("#select_id").val();
&nbsp;&nbsp;<span class="green">//获取Select选择的Value</span><br />
4. var checkIndex=$("#select_id ").get(0).selectedIndex;
&nbsp;&nbsp;<span class="green">//获取Select选择的索引值</span><br />
5. var maxIndex=$("#select_id option:last").attr("index");
&nbsp;&nbsp;<span class="green">//获取Select最大的索引值</span>
<br />
jQuery设置Select选择的Text和Value:
<br />
<input type="button" id="btn_index" class="button" value="设置Select索引值为1的项选中" />
<br />
<input type="button" id="btn_text" class="button" value="设置Select的Text值为jQuery的项选中" />
<br />
<input type="button" id="btn_value" class="button" value="设置Select的Value值为4的项选中" />
<br />
语法解释:
<br />
1. $("#select_id ").get(0).selectedIndex=1;
&nbsp;&nbsp;<span class="green">//设置Select索引值为1的项选中</span><br />
2. $("#select_id ").val(4); 
&nbsp;&nbsp;<span class="green">//设置Select的Value值为4的项选中</span><br />
3. $("#select_id option[text='jQuery']").attr("selected", true); 
&nbsp;&nbsp;<span class="green">//设置Select的Text值为jQuery的项选中</span><br />
<script type="text/javascript">
    $(
"#btn_index").click(function(){
        $(
"#slc1 ").get(0).selectedIndex=1;   
    });
    $(
"#btn_value").click(function(){
        $(
"#slc1 ").val(4);   
    });
    $(
"#btn_text").click(function(){
        $(
"#slc1 option[text='jQuery']").attr("selected"true); 
    });
</script>
</div>

<div class="div">
jQuery添加/删除Select的Option项:
<br />
<select id="slc2">
</select><br />
<input type="button" class="button" id="btn_add" value="追加Option项" />&nbsp;点击一次,Select将追加一个Option<br />
<input type="button" class="button" id="btn_insert_first" value="插入Option项" />&nbsp;点击将在Select第一个位置插入一个Option<br />
<input type="button" class="button" id="btn_del" value="删除Option项" />&nbsp;点击将删除Select中索引值最大Option(最后一个)<br />

<script type="text/javascript">
    
var i=1;
    
var bI=false;
    $(
"#btn_add").click(function(){
        $(
"#slc2").append("<option value='"+i+"'>"+i+"</option>");
        i
++;
    });
    $(
"#btn_insert_first").click(function(){
        
if (!bI)
        {
            $(
"#slc2").prepend("<option value='0'>请选择</option>");
            bI
=true;
        }
    });
    $(
"#btn_del").click(function(){
        $(
"#slc2 option:last").remove();
    });
</script>
语法解释:
<br />
1. $("#select_id").append("
&lt;option value='Value'>Text&lt;/option>");&nbsp;&nbsp;<span class="green">//为Select追加一个Option(下拉项)</span><br />
2. $("#select_id").prepend("
&lt;option value='0'>请选择&lt;/option>");&nbsp;&nbsp;<span class="green">//为Select插入一个Option(第一个位置)</span><br />
3. $("#select_id option:last").remove();
&nbsp;&nbsp;<span class="green">//删除Select中索引值最大Option(最后一个)</span><br />
4. $("#select_id option[index='0']").remove();
&nbsp;&nbsp;<span class="green">//删除Select中索引值为0的Option(第一个)</span><br />
5. $("#select_id option[value='3']").remove();
&nbsp;&nbsp;<span class="green">//删除Select中Value='3'的Option</span><br />
5. $("#select_id option[text='4']").remove();
&nbsp;&nbsp;<span class="green">//删除Select中Text='4'的Option</span><br />
</div>
<br />

</body>
</html>

 

代码
$("#selBrand").change(function(){  //二级动
                    $("#selSeries").empty();
                    //$("#selBrandkind").append("
<option value='a'>b</option><option value='b'>c</option>");
                    
                    $.ajax({
                          url: "getInfo.aspx?level=1
&kind=" +$("#selBrand").val(),
                          cache: false,
                          success: function(html){
                            $("#selBrandkind").append(html);
                          }
                        });

                    $("#selBrandkind").addClass("on");
                }); 

 

 

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!