现在已知值为2,想查询下下拉框的值,如果有value=2,那么这项就选中.代码如下:
<select id='S1'>
<option value='1'>aa</option>
<option value='2'>bb</option>
<option value='3'>cc</option>
</select>
其中bb的value='2',为其设选中状态加selected  ,
用jquery怎么写,高手指点:
$(document).ready(function(){
var s1="";
s1=$("#s1");
        });
?????????

解决方案 »

  1.   

    var objSelect = document.getElementById("S1");
    for(var i=0;i<objSelect.options.length;i++ ){
         if(objSelect.options[i].value=2){
              objSelect.options[i].selected=true;
              reuturn;
         }
      

  2.   


    1.//得到select项的个数     
    2.jQuery.fn.size = function(){     
    3.    return jQuery(this).get(0).options.length;     
    4.}     
    5.  
    6.//获得选中项的索引     
    7.jQuery.fn.getSelectedIndex = function(){     
    8.    return jQuery(this).get(0).selectedIndex;     
    9.}     
    10.  
    11.//获得当前选中项的文本     
    12.jQuery.fn.getSelectedText = function(){     
    13.    if(this.size() == 0) return "下拉框中无选项";     
    14.    else{     
    15.        var index = this.getSelectedIndex();           
    16.        return jQuery(this).get(0).options[index].text;     
    17.    }     
    18.}     
    19.  
    20.//获得当前选中项的值     
    21.jQuery.fn.getSelectedValue = function(){     
    22.    if(this.size() == 0)      
    23.        return "下拉框中无选中值";     
    24.         
    25.    else  
    26.        return jQuery(this).val();     
    27.}     
    28.  
    29.//设置select中值为value的项为选中     
    30.jQuery.fn.setSelectedValue = function(value){     
    31.    jQuery(this).get(0).value = value;     
    32.}     
    33.  
    34.//设置select中文本为text的第一项被选中     
    35.jQuery.fn.setSelectedText = function(text)     
    36.{     
    37.    var isExist = false;     
    38.    var count = this.size();     
    39.    for(var i=0;i<count;i++)     
    40.    {     
    41.        if(jQuery(this).get(0).options[i].text == text)     
    42.        {     
    43.            jQuery(this).get(0).options[i].selected = true;     
    44.            isExist = true;     
    45.            break;     
    46.        }     
    47.    }     
    48.    if(!isExist)     
    49.    {     
    50.        alert("下拉框中不存在该项");     
    51.    }     
    52.}     
    53.//设置选中指定索引项     
    54.jQuery.fn.setSelectedIndex = function(index)     
    55.{     
    56.    var count = this.size();         
    57.    if(index >= count || index < 0)     
    58.    {     
    59.        alert("选中项索引超出范围");     
    60.    }     
    61.    else  
    62.    {     
    63.        jQuery(this).get(0).selectedIndex = index;     
    64.    }     
    65.}     
    66.//判断select项中是否存在值为value的项     
    67.jQuery.fn.isExistItem = function(value)     
    68.{     
    69.    var isExist = false;     
    70.    var count = this.size();     
    71.    for(var i=0;i<count;i++)     
    72.    {     
    73.        if(jQuery(this).get(0).options[i].value == value)     
    74.        {     
    75.            isExist = true;     
    76.            break;     
    77.        }     
    78.    }     
    79.    return isExist;     
    80.}     
    81.//向select中添加一项,显示内容为text,值为value,如果该项值已存在,则提示     
    82.jQuery.fn.addOption = function(text,value)     
    83.{     
    84.    if(this.isExistItem(value))     
    85.    {     
    86.        alert("待添加项的值已存在");     
    87.    }     
    88.    else  
    89.    {     
    90.        jQuery(this).get(0).options.add(new Option(text,value));     
    91.    }     
    92.}     
    93.//删除select中值为value的项,如果该项不存在,则提示     
    94.jQuery.fn.removeItem = function(value)     
    95.{         
    96.    if(this.isExistItem(value))     
    97.    {     
    98.        var count = this.size();             
    99.        for(var i=0;i<count;i++)     
    100.        {     
    101.            if(jQuery(this).get(0).options[i].value == value)     
    102.            {     
    103.                jQuery(this).get(0).remove(i);     
    104.                break;     
    105.            }     
    106.        }             
    107.    }     
    108.    else  
    109.    {     
    110.        alert("待删除的项不存在!");     
    111.    }     
    112.}     
    113.//删除select中指定索引的项     
    114.jQuery.fn.removeIndex = function(index)     
    115.{     
    116.    var count = this.size();     
    117.    if(index >= count || index < 0)     
    118.    {     
    119.        alert("待删除项索引超出范围");     
    120.    }     
    121.    else  
    122.    {     
    123.        jQuery(this).get(0).remove(index);     
    124.    }     
    125.}     
    126.//删除select中选定的项     
    127.jQuery.fn.removeSelected = function()     
    128.{     
    129.    var index = this.getSelectedIndex();     
    130.    this.removeIndex(index);     
    131.}     
    132.//清除select中的所有项     
    133.jQuery.fn.clearAll = function()     
    134.{     
    135.    jQuery(this).get(0).options.length = 0;     
    136.}  
    //文章来自:http://gundumw100.javaeye.com/blog/543448
      

  3.   


    <script type="text/javascript">
    //<![CDATA[
    $(document).ready(function(){
    var $s1=$('#S1');
    $s1.val('2');
    })
    //]]>
    </script>
      

  4.   


    <!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>
    <meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
    <title>test</title><script src="page/js/jquery.min.js"></script>
    <script>
    $(function(){
     $("#btn").click(function(){
     var value = $("#text").val();
     $("#S1").children("option").each(function(){
        var temp_value = $(this).val();
      if(temp_value == value){
         $(this).attr("selected",true);
      }
     });
     });
    });
    </script>
    </head>
    <body>
    请输入要选择的数字<input type="text" id="text" /><input type="button" value="点击" id="btn" />
    <select id='S1'>
        <option value='1'>aa</option>
        <option value='2'>bb</option>
        <option value='3'>cc</option>
    </select>
    </body>
    </html>