javascript - 如何使用 Javascript 或 Jquery 获取所选选项的值?
问题描述
我创建了一个自定义选择框,我想获取所选标签的值,当我单击“检查”按钮时,我收到一条警报,显示 div 内的代码看起来合乎逻辑,问题是我怎么能得到所选选项的值或文本。
.selectbox_newpost {
padding:0;
margin:0;
height:100vh;
width:100vw;
font-family: sans-serif;
color:#FFF;
}
.selectbox_newpost {
display:flex;
flex-direction: column;
position:relative;
width:250px;
height:40px;
}
.option_newpost {
padding:0 30px 0 10px;
min-height:40px;
display:flex;
align-items:center;
background:#333;
border-top:#222 solid 1px;
position:absolute;
top:0;
width: 100%;
pointer-events:none;
order:2;
z-index:1;
transition:background .4s ease-in-out;
box-sizing:border-box;
overflow:hidden;
white-space:nowrap;
}
<!DOCTYPE html>
<html lang="en" dir="ltr">
<head>
<link rel="stylesheet" href="stylesss.css">
<script src="https://code.jquery.com/jquery-3.6.0.js" integrity="sha256-H+K7U5CnXl1h5ywQfKtSj8PCmoN9aaq30gDh27Xc0jk=" crossorigin="anonymous"></script>
<meta charset="utf-8">
<title>test</title>
</head>
<body>
<script type="text/javascript">
function clicked(){
var option = document.getElementById("selectedOptionNewpost");
alert(option.innerHTML);
}
</script>
<div class="selectbox_container_newpost">
<div id="selectedOptionNewpost" class="selectbox_newpost" tabindex="1">
<input class="selectopt" name="test" type="radio" id="opt1" checked>
<label for="opt1" class="option_newpost">Objects</label>
<input class="selectopt" name="test" type="radio" id="opt2">
<label for="opt2" class="option_newpost">Vehicules</label>
<input class="selectopt" name="test" type="radio" id="opt3">
<label for="opt3" class="option_newpost">Technology</label>
<input class="selectopt" name="test" type="radio" id="opt4">
<label for="opt4" class="option_newpost">Books</label>
<input class="selectopt" name="test" type="radio" id="opt5">
<label for="opt5" class="option_newpost">Furniture</label>
<input class="selectopt" name="test" type="radio" id="opt6">
<label for="opt6" class="option_newpost">Others...</label>
</div>
</div>
<button onclick="clicked()">Check</button>
</body>
</html>
解决方案
您可以找到选定的单选按钮,然后找到与其关联的标签并打印出 textContent。
检查下面
请让我知道这是否是您要找的。
.selectbox_newpost {
padding:0;
margin:0;
height:100vh;
width:100vw;
font-family: sans-serif;
color:#FFF;
}
.selectbox_newpost {
display:flex;
flex-direction: column;
position:relative;
width:250px;
height:40px;
}
.option_newpost {
padding:0 30px 0 10px;
min-height:40px;
display:flex;
align-items:center;
background:#333;
border-top:#222 solid 1px;
position:absolute;
top:0;
width: 100%;
pointer-events:none;
order:2;
z-index:1;
transition:background .4s ease-in-out;
box-sizing:border-box;
overflow:hidden;
white-space:nowrap;
}
<!DOCTYPE html>
<html lang="en" dir="ltr">
<head>
<link rel="stylesheet" href="stylesss.css">
<script src="https://code.jquery.com/jquery-3.6.0.js" integrity="sha256-H+K7U5CnXl1h5ywQfKtSj8PCmoN9aaq30gDh27Xc0jk=" crossorigin="anonymous"></script>
<meta charset="utf-8">
<title>test</title>
</head>
<body>
<script type="text/javascript">
function clicked(){
var option = document.getElementById("selectedOptionNewpost");
const radioSelected = option.querySelector("input[type='radio']:checked")
const labelForSelectedRadio = option.querySelector(`label[for=${radioSelected.getAttribute('id')}]`)
console.log(labelForSelectedRadio.textContent)
}
</script>
<div class="selectbox_container_newpost">
<div id="selectedOptionNewpost" class="selectbox_newpost" tabindex="1">
<input class="selectopt" name="test" type="radio" id="opt1" checked>
<label for="opt1" class="option_newpost">Objects</label>
<input class="selectopt" name="test" type="radio" id="opt2">
<label for="opt2" class="option_newpost">Vehicules</label>
<input class="selectopt" name="test" type="radio" id="opt3">
<label for="opt3" class="option_newpost">Technology</label>
<input class="selectopt" name="test" type="radio" id="opt4">
<label for="opt4" class="option_newpost">Books</label>
<input class="selectopt" name="test" type="radio" id="opt5">
<label for="opt5" class="option_newpost">Furniture</label>
<input class="selectopt" name="test" type="radio" id="opt6">
<label for="opt6" class="option_newpost">Others...</label>
</div>
</div>
<button onclick="clicked()">Check</button>
</body>
</html>
推荐阅读
- javascript - 在烤面包机 onShown 事件中调用其他函数
- c# - 单击单元格时如何更改数据网格中的单元格值
- c - C 语言中的套接字编程:无法将消息从 FreeBSD 客户端广播到 Linux 服务器。广播 IP 应为 255.255.255.255
- vue.js - 模板内的Vue for循环
- ruby-on-rails - SAAS 应用架构微服务 vs 单体
- sql - SQL中按组连续出现的值的最大次数
- android - 阻止我的应用程序复制 textview 的文本
- android - Android viewpager 滞后于 recyclerviews
- java - 我不明白为什么我的计算不起作用
- python - 将带有参数的 Python 装饰器合并为一个