HTML select 操作

今天遇到一个问题,就是想设置select的默认选择项。但是试了很多方法都不行:

<fieldset data-role="contractstatus">  
        <label for="switch">是否中标:</label>
        <select name="switch" id="switch">
          <option value="1">NO</option>
          <option value="2">YES</option>
        </select>
        </fieldset>

jquery脚本1:

 <script type="text/javascript">
            $(function(){
                try{
                <%   int k=Integer.parseInt(p_status); 
                        if(k==1){
                %>
                //$("#switch option[text='YES']").attr("selected", "selected");
                //$("#switch").val("2");
                $("#switch").find("option[value='1']").attr("selected","selected");
                //$("#switch").find("option[value='2']").focus();
                <%
                        }else if(i==2){
                %>
                $("#switch option[text='YES']").attr("selected", "selected"HTML源码编辑器);
                <%
                        }
                %>
                }catch(e){}
            });
</script> 

jquery脚本2:

<script type="text/javascript">
            $(function(){
                setTimeout(function() { 
                <%   int k=Integer.parseInt(p_status); 
                        if(k==1){
                %>
                $("#switch option[text='YES']").attr("selected", true);
                //$("#switch").val("2");
                <%
                        }else if(i==2){
                %>
                $("#switch option[text='YES']").attr("selected", "selected");
                <%
                        }
                %>
                }, 1);
            });
</script>

试了各种方法都不行

最后发现,其实select已经改变了,只是对应的文本变,所以只需修改文本就行。

$("#switch option[value='1']").attr("selected", "selected");
//$("#switch").selectmenu("refresh", true);
$("#switch").slider("refresh"); 

 

posted @ 2015-12-15 13:18  swack  阅读(580)  评论(0编辑  收藏  举报