JQuery获取和设置select下拉框的值

    获取Select :
     获取select 选中的 text :
       $("#sid").find("option:selected").text();
     
     获取select选中的 value:
       $("#sid").val();
     
     获取select选中的索引:
         $("#sid").get(0).selectedIndex;
     
    设置select:
     设置select 选中的索引:
         $("#sid").get(0).selectedIndex=index;//index为索引值
     
     设置select 选中的value:
        $("#sid").attr("value","Normal“);
        $("#sid").val("Normal");
        $("#sid").get(0).value = value;
     
     设置select 选中的text:
    var count=$("#sidoption").length;
      for(var i=0;i<count;i++)  
         {           if($("#sid").get(0).options[i].text == text)  
            {  
                $("#sid").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 :
    $("#sid").empty();
     
    获取radio的值:
    <script language="javascript">
        $(function() {
            $("#Button1").click(function() {
                var r = $("input:checked").attr("id");
                alert(r);
     
                r = $(":radio:checked").attr("id");
                alert(r);
     
                r = $(":radio:checked").eq(0).attr("id");
                alert(r);
     
                r = $("#div1").children(":radio:checked").attr("id");
                alert(r);
     
                r = $("#div1").children(":radio:checked").eq(0).attr("id");
                alert(r);
     
                r = $("#div1").find(":radio:checked").eq(0).attr("id");
                alert(r);
     
                $("#div1").find(":radio").each(function() {
                    alert($(this).attr("checked"));
                    if ($(this).attr("checked")) {
                        r = $(this).attr("id");
                        alert(r);
                    }
                });
            });
        });
    </script>
     
    <div id='div1'>
    <input id="Radio1" name='rad' type="radio" />1<br/>
    <input id="Radio2" name='rad' type="radio" />2
    </div>
        <input id="Button1" type="button" value="button" />

 

posted @ 2017-02-09 10:01  侠岚之弋痕夕  阅读(1463)  评论(0编辑  收藏  举报
Where is the starting point, we don't have a choice, but the destination where we can pursue!