JS动态获取select中被选中的option的值,并在控制台输出
生活城市:
<select id="province">
<option>河南省</option>
<option>黑龙江省</option>
<option></option>
</select>
在script中获取被选中的option的值:
var province = document.querySelector("#province");
//select对象集合options返回包含下拉列表所有选项
//selectedIndex 代表的是你所选中项的index
var value = province.options[province.selectedIndex;].value;
在控制台输出被选中的option的值:
function output () {
//selectedIndex 代表的是你所选中项的index
var value = province.options[province.selectedIndex].value;
var index = province.selectedIndex;
console.log(value);
}
output();
province.addEventListener('change', output);
由于被选中的option的值是动态的,所以要把index 和 value定义在函数内部,让它们的值随着事件的发生而更新。
页面刚被渲染出来的时候,我们没有点击select选项,但它会有一个默认值,所以我们要在事件被触发前,执行一次函数。
把多次使用的函数进行封装可以减少代码的冗余。
本人前端新手一枚,如果哪里写的不对,欢迎在评论区留言!
JS动态获取select中被选中的option的值,并在控制台输出的更多相关文章
- 怎样用jQuery拿到select中被选中的option的值
1.首先要保证select中每一个option标签都有value属性: 2.jquery的写法 1 $('#sele').val()//这里假设select的id是sele,这样可以获取当前选中的op ...
- js,jquery获取下拉框选中的option
js获取select选中的值: var sel=document.getElementById("select1"); var index = sel.selectedIndex; ...
- js动态获取select选中的option
最近在写报表管理模块时,需要通过条件去筛选符合条件的数据,筛选条件用的布局有select,input等.在调试的过程中一直获取不到select选中的option.于是就查询些资料,发现用select的 ...
- jQuery获取循环中的选中单选按钮radio的值
1.<input type="radio" name="testradio" value="jquery获取radio的值" /> ...
- 利用jquery获取html中被选中的input的值
单个按钮 <div id="wrap"> <input type="radio" name="payMethod" val ...
- 如何获取select中的value、text、index相关值 && 如何获取单选框中radio值 && 触发事件 && radio 默认选中
如何获取select中的value.text.index相关值 select还是比较常用的一个标签,如何获取其中的内容呢? 如下所示: <select id="select" ...
- js 获取select的值 / js动态给select赋值
jQuery获取Select选择的Text和Value:语法解释:1. $("#select_id").change(function(){//code...}); //为Se ...
- JS中如何获取<Select>中value和text的值
原文地址:JS中如何获取<Select>中value和text的值 html代码: <select id = "city" onchange="chan ...
- JS动态改变select选择变更option的index值
document.getElementById("louyuming").options[0].selected=true; function jsSelectIsExitItem ...
随机推荐
- Distinct Substrings SPOJ - DISUBSTR 后缀数组
Given a string, we need to find the total number of its distinct substrings. Input T- number of test ...
- Array Transformer UVA - 12003
题目:传送门 题意: 给你n个数,要进行m次操作 对于每次操作(l,r,v,p)代表:在区间[l,r]中有x(这个x是需要你自己找出来的)个数小于v,你需要把序列的第p个位置的值改成u∗k/(r−l ...
- 2.PowerShell概述
PowerShell PowerShell命令窗一般随系统带着,运行->输入:powershell,即可打开命令窗口. 命令 Powershell有诸多命令,兼容cmd命令 语法和命令 在此我推 ...
- Linux运维博客大全
系统 U盘安装Linux详细步骤_hanxh7的博客-CSDN博客_u盘安装linux 使用U盘安装linux系统 - lwenhao - OSCHINA 各厂商服务器存储默认管理口登录信息(默认IP ...
- Automatic merge failed; fix conflicts and then commit the result.解决方法
产生原因: git pull 的时候会分为两步,第一步先从远程服务器上拉下代码,第二步进行merge.当你merge时候失败了就会产生Automatic merge failed; fix confl ...
- mysql(四)------Mysql中的锁
1. 2 MySQL InnoDB 锁的基本类型 https://dev.mysql.com/doc/refman/5.7/en/innodb-locking.html 官网把锁分成了 8 类.所以我 ...
- 你不知道的 JS (系列丛书) - 第二版
你不知道的 JS (系列丛书) - 第二版 You Don't Know JS (book series) - 2nd Edition https://github.com/learning-js-b ...
- Object 循环引用 All In One
Object 循环引用 All In One circular reference bug var a = {}; a.a = a; refs deep copy bug https://segmen ...
- MongoDB 101
MongoDB 101 Studio 3T https://studio3t.com/academy/ https://studio3t.com/academy/lessons/introducing ...
- bye MVA
bye MVA https://mva.microsoft.com/