select获取及选中

<!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>
    <title>无标题页</title>

    <script type="text/javascript" src="JS/jquery-1.4.1.min.js"></script>

    <script type="text/javascript">
    $(function(){
        var xzVal = "3,5,6".split(',');
        $("#fruits").children().each(function(){
            for(var i = 0; i < xzVal.length; i++)
            {
                if($(this).val() == xzVal[i])
                    $(this).attr("selected", "selected");
            }
        });        
    });
    function getXZValue(){
        alert($("#fruits").val());
    }
    </script>

</head>
<body>
    <select name="fruits" id="fruits" size="10" multiple="multiple">
        <option value="1">苹果</option>
        <option value="2"></option>
        <option value="3">桃子</option>
        <option value="4">香蕉</option>
        <option value="5">菠萝</option>
        <option value="6">桔子</option>
    </select><input type="button" onclick="getXZValue();" value="获取" />
   
</body>
</html>

 

posted @ 2012-12-19 10:14  一千零一夜  阅读(216)  评论(0编辑  收藏  举报