欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

JS获取select的HTML,并获取当前的选中

程序员文章站 2022-03-26 17:56:18
...
<!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>

 

相关标签: JS Web js