如何从选定的li中获取值?

时间:2017-09-20 03:41:30

标签: javascript jquery html css

我从CodePen复制了一个水平选择菜单,这里是source code

结果如下:



var btn = document.querySelector("button")
var dropdown = document.querySelector(".dropdown-options")
var optionLinks = document.querySelectorAll(".option a")
console.log(optionLinks)

btn.addEventListener("click", function(e) {
  e.preventDefault()
  console.log("btn")
  dropdown.classList.toggle("open")
});

var clickFn = function(e) {
  e.preventDefault()
  dropdown.classList.remove("open")
  btn.innerHTML = this.text
  var activeLink = document.querySelector(".option .active")

  if (activeLink) {
    activeLink.classList.remove("active")
  }
  this.classList.add("active")
}
for (var i = 0; i < optionLinks.length; i++) {
  optionLinks[i].addEventListener("mousedown", clickFn, false)
}
&#13;
.container {
  max-width: 500px;
  margin-top: 10px;
  margin-left: 0px;
}
.container button {
  position: relative;
  background: none;
  border: none;
  outline: none;
  font-size: 16px;
  border-bottom: 2px solid #9b9b9b;
  padding-bottom: 8px;
  min-width: 150px;
  text-align: left;
  outline: none;
  cursor: pointer;
  z-index: 2;
}
.container button:after {
  content: '▾';
  position: absolute;
  right: 0px;
  top: 0%;
  color: rgb(142, 142, 142);
}
.container .dropdown {
  position: relative;
}
.container .dropdown .dropdown-options {
  list-style: none;
  margin: 0;
  padding: 0;
  background: white;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.24);
  display: inline-block;
  position: absolute;
  left: 0;
  bottom: 0;
  opacity: 0;
  -webkit-transform: scale(0.8) translate3d(-20px, 0px, 0);
          transform: scale(0.8) translate3d(-20px, 0px, 0);
  -webkit-transition: opacity 0.1s cubic-bezier(0.5, 2, 0.5, 0.75), -webkit-transform 0.3s cubic-bezier(0.5, 2, 0.5, 0.75);
  transition: opacity 0.1s cubic-bezier(0.5, 2, 0.5, 0.75), -webkit-transform 0.3s cubic-bezier(0.5, 2, 0.5, 0.75);
  transition: opacity 0.1s cubic-bezier(0.5, 2, 0.5, 0.75), transform 0.3s cubic-bezier(0.5, 2, 0.5, 0.75);
  transition: opacity 0.1s cubic-bezier(0.5, 2, 0.5, 0.75), transform 0.3s cubic-bezier(0.5, 2, 0.5, 0.75), -webkit-transform 0.3s cubic-bezier(0.5, 2, 0.5, 0.75);
  -webkit-transform-origin: 0 100%;
  transform-origin: 0 100%;
  z-index: 1;
}
.container .dropdown .dropdown-options.open {
  opacity: 1;
  -webkit-transform: scale(1) translate3d(0, 0, 0);
          transform: scale(1) translate3d(0, 0, 0);
  z-index: 5;
}
.container .dropdown .dropdown-options li {
  display: inline-block;
}
.container .dropdown .dropdown-options li a {
  text-decoration: none;
  display: inline-block;
  padding: 10px 16px;
  color: #2975DA;
}
.container .dropdown .dropdown-options li a:hover {
  color: #2269c7;
}
.container .dropdown .dropdown-options li a.active {
  border-bottom: 2px solid #9b9b9b;
  color: #9b9b9b;
}
.container .dropdown .dropdown-options li a.active:hover {
  color: #9b9b9b;
}
&#13;
<div class="container">
  <div class="dropdown">
    <button>Type</button>
    <ul class="dropdown-options">
      <li class="option">
        <a href="#">A</a>
      </li>
      <li class="option">
        <a href="#">B</a>
      </li>
      <li class="option">
        <a href="#">C</a>
      </li>
      <li class="option">
        <a href="#">D</a>
      </li>
    </ul>
  </div>
</div>
&#13;
&#13;
&#13;

在提交此表单之前,我遇到了如何获取selected li的问题。它不是<select>我在表单中使用此菜单,是否有一种简单的方法可以使用li获取JavaScript的选定值?

2 个答案:

答案 0 :(得分:1)

您可以使用解决方案https://jsfiddle.net/9Lk8vwvL/

var btn = document.querySelector("button")
var dropdown = document.querySelector(".dropdown-options")
var optionLinks = document.querySelectorAll(".option a")

btn.addEventListener("click", function(e) {
  e.preventDefault()
  dropdown.classList.toggle("open")
});

var clickFn = function(e) {
  console.log(e.target.innerText);
  e.preventDefault()
  dropdown.classList.remove("open")
  btn.innerHTML = this.text
  var activeLink = document.querySelector(".option .active")

  if (activeLink) {
    activeLink.classList.remove("active")
  }
  this.classList.add("active")
}
for (var i = 0; i < optionLinks.length; i++) {
  optionLinks[i].addEventListener("mousedown", clickFn, false)
}
.container {
  max-width: 500px;
  margin-top: 10px;
  margin-left: 0px;
}
.container button {
  position: relative;
  background: none;
  border: none;
  outline: none;
  font-size: 16px;
  border-bottom: 2px solid #9b9b9b;
  padding-bottom: 8px;
  min-width: 150px;
  text-align: left;
  outline: none;
  cursor: pointer;
  z-index: 2;
}
.container button:after {
  content: '▾';
  position: absolute;
  right: 0px;
  top: 0%;
  color: rgb(142, 142, 142);
}
.container .dropdown {
  position: relative;
}
.container .dropdown .dropdown-options {
  list-style: none;
  margin: 0;
  padding: 0;
  background: white;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.24);
  display: inline-block;
  position: absolute;
  left: 0;
  bottom: 0;
  opacity: 0;
  -webkit-transform: scale(0.8) translate3d(-20px, 0px, 0);
          transform: scale(0.8) translate3d(-20px, 0px, 0);
  -webkit-transition: opacity 0.1s cubic-bezier(0.5, 2, 0.5, 0.75), -webkit-transform 0.3s cubic-bezier(0.5, 2, 0.5, 0.75);
  transition: opacity 0.1s cubic-bezier(0.5, 2, 0.5, 0.75), -webkit-transform 0.3s cubic-bezier(0.5, 2, 0.5, 0.75);
  transition: opacity 0.1s cubic-bezier(0.5, 2, 0.5, 0.75), transform 0.3s cubic-bezier(0.5, 2, 0.5, 0.75);
  transition: opacity 0.1s cubic-bezier(0.5, 2, 0.5, 0.75), transform 0.3s cubic-bezier(0.5, 2, 0.5, 0.75), -webkit-transform 0.3s cubic-bezier(0.5, 2, 0.5, 0.75);
  -webkit-transform-origin: 0 100%;
  transform-origin: 0 100%;
  z-index: 1;
}
.container .dropdown .dropdown-options.open {
  opacity: 1;
  -webkit-transform: scale(1) translate3d(0, 0, 0);
          transform: scale(1) translate3d(0, 0, 0);
  z-index: 5;
}
.container .dropdown .dropdown-options li {
  display: inline-block;
}
.container .dropdown .dropdown-options li a {
  text-decoration: none;
  display: inline-block;
  padding: 10px 16px;
  color: #2975DA;
}
.container .dropdown .dropdown-options li a:hover {
  color: #2269c7;
}
.container .dropdown .dropdown-options li a.active {
  border-bottom: 2px solid #9b9b9b;
  color: #9b9b9b;
}
.container .dropdown .dropdown-options li a.active:hover {
  color: #9b9b9b;
}
<div class="container">
  <div class="dropdown">
    <button>Type</button>
    <ul class="dropdown-options">
      <li class="option">
        <a href="#">A</a>
      </li>
      <li class="option">
        <a href="#">B</a>
      </li>
      <li class="option">
        <a href="#">C</a>
      </li>
      <li class="option">
        <a href="#">D</a>
      </li>
    </ul>
  </div>
</div>

由于您在clickFn方法中有活动,因此可以使用console.log(e.target.innerText);

希望这可以帮助您获得所选的li值。

答案 1 :(得分:1)

clickFn功能中,您已将所选链接设为button innerHTML - btn.innerHTML = this.text;

button的默认文字为Type。您可以在表单提交期间检查按钮文本,如:

if (btn.innerText === "Type")
   //Option not selected - Alert user
else
   //Option selected - Validate other fields