JS獲取select的HTML,並獲取當前的選中

<!DOCTYPE>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=EmulateIE7" />
    <meta name="keywords" content="" />
    <meta name="description" content="" />
    <title>待辦事項</title>
    <link rel="shortcut icon" href="images/favicon.ico" />
    <link rel="stylesheet" type="text/css" href="style/base.css" />
    <link rel="stylesheet" type="text/css" href="style/common.css" />
</head>
<body>
<select name="post_address_1">
    <option value="">選擇地區</option>
    <volist name="area_list" id="vo">
        <option value="{$vo.id}">{$vo.name}</option>
    </volist>
</select>
<script>
    /**
     * 獲取select的HTML內容,並獲取選中信息
     */
    function set_select() {
        var address_1 = $("select[name='post_address_1']").val();
        $("select[name='post_address_1'] option").each(function(index,ele){
            if(ele.getAttribute('value') == address_1){
                $(ele).attr("selected",true);
            }else{
                $(ele).attr("selected",false);
            }
        });
        var post_address_1 = $("select[name='post_address_1']").html();
        console.log(post_address_1);
    }
</script>
</body>
</html>

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章