首页 > 解决方案 > 如何使用 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>

标签: javascripthtmljquerycss

解决方案


您可以找到选定的单选按钮,然后找到与其关联的标签并打印出 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>


推荐阅读