select-option

来源:互联网 发布:java集合转换为字符串 编辑:程序博客网 时间:2024/05/29 14:04

小伙伴求助如何在加载时获取select的值,其实不难;
只需要给option 加上selected这个属性就行;

<!DOCTYPE html><html lang="en"><head>    <meta charset="UTF-8">    <title>Title</title>    <script src="jquery-3.2.1.min.js"></script></head><body><select name="" id="select_child" >    <option value="hello1" selected>hello1</option>    <option value="hello2">hello</option>    <option value="hello3">hello</option></select><button id="btn"></button><script>       此处写代码</script></body></html>

这里脚本这样写即可;

$(            console.log($('#select_child').val())        )

当然这是使用jquery;如果用原生js,怎么写呢?

window.onload=function(){        console.log(document.getElementById("select_child").value)    }

select其实是可以绑定change事件,可以这样写;

$('#select_child').change(function(){            console.log(this.value)        });

链式操作能够提高性能,能用原生就能写的地方还是用原生比较好,这样占用栈资源;

var selecEle=document.getElementById("select_child");        selecEle.onchange=function(){            console.log(this.value)        }
原创粉丝点击