zl程序教程

您现在的位置是:首页 >  其它

当前栏目

select标签

标签 SELECT
2023-06-13 09:17:36 时间

select标签

网页中的下拉选择器。

一、语法

选择框<select>标签:主要用于数据的选择,比如说我们常常在一些网站上填写:省份、城市、地区的时候,使用的就是select

<form>
	<selectname="province">
        <optionvalue="1">北京</option>
        <optionvalue="2"selected>上海</option>
    </select>
</form>
  • 属性 name:我们提交数据的时候,后台根据 name 来取前端传过去的数据
  • <option>一起使用,option 就是 select 中的一个个选项。
    • 属性 value:就是选择了的数据,并提交到后台的
    • 属性 selected,表明是否选中,也就是在 select 框中显示的那个选项
    • option 中的 value 属性表示 select 要提交的值
  • 一般结合 form 标签使用

二、代码实战

新建 html 文件 14-select.html ,编写下方程序,运行看看效果吧

<!DOCTYPE html>
<htmllang="en">
    <head>
        <metacharset="UTF-8">
        <metahttp-equiv="X-UA-Compatible"content="IE=edge">
        <metaname="viewport"content="width=device-width, initial-scale=1.0">
        <title>select标签</title>
    </head>
  
    <body>
        <formaction="/submit.do"method="get">
            <div>地址:
                <selectname="address"onchange="handleChange()">
                    <optionvalue="1">北京</option>
                    <optionvalue="2"selected>上海</option>
                    <optionvalue="3">天津</option>
                </select>
            </div>
            <buttontype="submit">提交</button>
        </form>

        <scripttype="text/javascript">
            function handleChange(){
                alert('地址修改了')
            }
        </script>
    </body>
</html>