jquery遍历select元素(实例讲解)


本篇文章提供一款jquery遍历select教程代码,主要是利用了$("#<%=ddl_xreg_id.clientid%> option").each(function() {形式来each遍历一次,这样所有的select就给查询了一次。

复制代码 代码如下:

<script src="jquery-1.2.1.js" type="text/网页特效"></script>
<script language="javascript" type="text/javascript">
$(document).ready(function(){
$("#selecttest").change(function()
{
       //alert("hello");
       //alert($("#selecttest").attr("name"));
       //$("a").attr("href","xx.html");
       //window.location.href="xx.html";
       //alert($("#selecttest").val());
       alert($("#selecttest option[@selected]").text());
       $("#selecttest").attr("value", "2");

});
});
</script>


<a href="#">aaass</a>

<!--下拉框-->
<select id="selecttest" name="selecttest">
<option value="1">11</option>
<option value="2">22</option>
<option value="3">33</option>
<option value="4">44</option>
<option value="5">55</option>
<option value="6">66</option>
</select>

方法二
function autoscrollregion() {
        var reg_name = $("#<%=txt_reg_name.clientid%>").val();

        $("#<%=ddl_xreg_id.clientid%> option").each(function() {
        if ($(this).text() == reg_name) {
                $(this).attr("selected", "selected");
                break;
            }
        });     
    }


« 
» 
快速导航

Copyright © 2016 phpStudy | 豫ICP备2021030365号-3