<script src="jquery.js" language="javascript"></script><span id="xx">广州市</span><select id="select_id">
    <option>北京市</option>
    <option>广州市</option>
    <option>上海市</option>
</select>如何让select 选中 广州市 ?百度上搜到 $("#select_id option[text='广州市']").attr("selected", true);  但用不了><!!!

解决方案 »

  1.   


    <script>
    //普通js写法
    document.getElementById("select_id").options[1].setAttribute("selected", true);
    //jquery应该这样写
    $("#select_id option").eq(1).attr("selected",true);
    </script>
      

  2.   

    给每个option加上value属性。。<span id="xx">广州市</span><select id="select_id">
      <option value="北京市">北京市</option>
      <option value="广州市">广州市</option>
      <option value="上海市">上海市</option>
    </select>
    <script>
       $("#select_id option[value='广州市']").attr("selected", true);
    </script>或者<script>
    $("#select_id option).each(function(){
       if($(this).text() === '广州市'){
          $(this).attr('selected', 'selected);
       }
    });
    </script>
      

  3.   


    改为$("#select_id option[text='广州市']").attr("selected", "selected"); 
      

  4.   

    <html>
     <body>
      <select id="selectedId">
        <option value="1" >天津</option>
        <option value="2"> 北京</option>
        <option value="3">上海</option>
      </select>
     </body>
     <script src="jquery.js" type="text/javascript"></script>
     <script>
     // $("#selectedId").val("3");
     $("#selectedId").find("option").eq(2).attr("selected","selected")
     </script>
    </html>
    上面的都已经经过IE8。FIFIREFOX 测试都正常