单击另一个div打开列表下拉列表

时间:2019-06-18 07:33:23

标签: javascript jquery html

我有关于下拉选择选项的代码,单击

时,我必须尝试我的代码工作
<div class="field-placeholder"><span>Select Yuor Subject</span></div>

下拉菜单将展开列表选项,但是现在我必须双击同一点以展开选项

我尝试使用 dblclick() 鼠标移到() 但不起作用

任何想法 这是我的工作

https://jsfiddle.net/ravizikrillah/m6Lc5jwn/2/

1 个答案:

答案 0 :(得分:4)

您可以通过添加一个/两个CSS规则并删除事件侦听器来实现。将以下属性添加到.field-placeholder

.field-wrapper .field-placeholder{
  /* ... */

  user-select: none;
  pointer-events: none;
}

pointer-events规则将关闭元素上的鼠标事件,因此将其变为隐藏元素。

user-select阻止用户选择元素的内容。

然后,您可以删除click上的.field-wrapper .field-placeholder事件处理程序。

/*
You can remove the following:

$(".field-wrapper .field-placeholder").on("click", function () {
   $(this).closest(".field-wrapper").find("select").focus(); 
   $(this).closest(".field-wrapper").find(".mySelect").click(); 
});
*/

$('.mySelect').on('change', function() {
  var value1 = $('.mySelect').val();
  if (value1) {
 		$(this).closest(".field-wrapper").addClass("hasValue");
  } else {
    $(this).closest(".field-wrapper").removeClass("hasValue");
  }
});
.field-wrapper{
  position: relative;
  margin-bottom: 30px;
}
.field-wrapper select{
  border: 1px solid #DADCE0;
  padding: 15px;
  border-radius: 4px;
  width: 100%;
  height: 100%;
}
.field-wrapper .field-placeholder{
  font-size: 16px;
  position: absolute;
  bottom: 17px;
  -webkit-box-sizing: border-box;
  box-sizing: border-box;
  color: #80868b;
  left: 8px;
  padding: 0 8px;
  -webkit-transition: transform 150ms cubic- bezier(0.4,0,0.2,1),opacity 150ms cubic-bezier(0.4,0,0.2,1);
  transition: transform 150ms cubic-bezier(0.4,0,0.2,1),opacity 150ms cubic-bezier(0.4,0,0.2,1);
  z-index: 1;
  text-align: left;
  width: 100%;
  
  /* Add these rules: */
  user-select: none;
  pointer-events: none;
}
.field-wrapper .field-placeholder span{
  background: #ffffff;
  padding: 0px 5px;
}

.field-wrapper select:not([disabled]):focus~.field-placeholder{
  color: #80868b;
}
.field-wrapper select:not([disabled]):focus~.field-placeholder,
.field-wrapper.hasValue select:not([disabled])~.field-placeholder{
  -webkit-transform: scale(.75) translateY(-39px) translateX(-60px);
  transform: scale(.75) translateY(-39px) translateX(-60px);
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="field-wrapper">
   <select name="subject" class="form-control-modified mySelect">
     <option disabled="disabled" selected="selected" value="">Number (required)</option>
     <option>1</option>
     <option>2</option>
     <option>3</option>                                               
  </select>
  <div class="field-placeholder"><span>Select Your Subject</span></div>
 </div>