实现点击下拉菜单下拉菜单不关闭,且可以将选中值以,为间隔写入输入框

首先
HTML

<div class="dropdown">
    <input type="text" autocomplete="off" id="textSelect" class="form-control inputWid"
     v-model="scvideo.lableName" style="width: 690px" data-toggle="dropdown" />
     	<ul class="dropdown-menu" role="menu" aria-labelledby="dropdownMenu1">
     		<li role="presentation" v-for="u in lableList" :value ="u.lableName" 
     		style="width: 690px;" data-stopPropagation="true" >
     			<a data-stopPropagation="true" role="menuitem" tabindex="-1" 
     			href="#" @click="lableInput(u.lableName)">嘻嘻</a>
     		</li>
     		<li role="presentation" v-for="u in lableList" :value ="u.lableName" 
     		style="width: 690px;" data-stopPropagation="true" >
     			<a data-stopPropagation="true" role="menuitem" tabindex="-1" 
     			href="#" @click="lableInput(u.lableName)">哈哈</a>
     		</li>
     		<li role="presentation" v-for="u in lableList" :value ="u.lableName" 
     		style="width: 690px;" data-stopPropagation="true" >
     			<a data-stopPropagation="true" role="menuitem" tabindex="-1" 
     			href="#" @click="lableInput(u.lableName)">啦啦</a>
     		</li>
     	</ul>
</div>

控制点击下拉不关闭的JS

//指定要操作的元素的click事件停止传播—定义属性值data-stopPropagation的元素点击时停止传播事件
    $("body").on('click','[data-stopPropagation]',function (e) {
        e.stopPropagation();
    });

以上的代码复制粘贴即可实现输入框带下拉框的功能,且可以点击下拉不会消失,只有点击其他位置会消失。

将点击的选项写入input框很简单,这里推荐一种VUE写法,需要其他的可以留言。

//将选择的标签写入input框
lableInput: function (lableName) {
   if (this.scvideo.lableName == null || this.scvideo.lableName == "") {
      this.scvideo.lableName = lableName;
   } else {
      this.scvideo.lableName = this.scvideo.lableName + "," + lableName;
   }
},

你可能感兴趣的:(前端,javascript,html,jquery)