Use bootstrap's drop-down menu to implement select drop-down checkbox multiple selection

CSS code:

/*Multiple selection drop-down box style (adjust according to your own style)*/
.dropdown_item{width: 100%}
.dropdown_item>li:HOVER{background-color: #eee;cursor: pointer;}
.dropdown_item>li {display: block;padding: 3px 10px;clear: both;font-weight: normal;line-height: 1.428571429;color: #333;white-space: nowrap;}
.dropdown_item>li>.check_box{width: 18px;height: 18px;vertical-align: middle;margin: 0px;}
.dropdown_item>li>span{vertical-align: middle;}
.select_multiple .caret{border-top: 4px solid!important;border-bottom: 0;}

HTML code:

<div class="dropup" style="position: relative;">
	<button class="btn btn-default dropdown-toggle form-control select_multiple" style="width: 100%;margin-left: 0px;" type="button" id="dropdownMenu21" data-toggle="dropdown">
    	<span class="select_text" data-is-select="false">Dro pup</span>
    	<span class="caret"></span>
  	</button>
  	<ul class="dropdown-menu dropdown_item" style="bottom: auto;">
    	<li><input type="checkbox" class="check_box" value="aa" /> <span>Action</span></li>
    	<li><input type="checkbox" class="check_box" value="bb"/> <span>Another action</span></li>
    	<li><input type="checkbox" class="check_box" value="cc"/> <span>Something else here</span></li>
    	<li><input type="checkbox" class="check_box" value="dd"/> <span>Separated link</span></li>
  	</ul><!-- In order to facilitate the demonstration, the type is set to text, which can be set to hidden in practice -->
	<input type="text" name="" class="select_val"/>
</div>

JS code:

//Multiple selection drop-down box implementation
    $(document).on("click",".check_box",function(event){
		event.stopPropagation();//Prevent event bubbling to prevent triggering li's click event
		// checked item
		var $selectTextDom=$(this).parent().parent("ul").siblings("button").children(".select_text");
		// check the value of the option
		var $selectValDom=$(this).parent().parent("ul").siblings(".select_val");
		//Is there an option
		var isSelected=$selectTextDom[0].getAttribute("data-is-select");
		var selectText="";//Text value for display
		var selectVal=$selectValDom.val();//The actual value will be submitted to the background
		var selected_text=$(this).siblings("span").text();//The text value of the current check
		var selected_val=$(this).val();//The actual value of the current check
		// Determine if it has been selected
		if(isSelected=="true"){
			selectText=$selectTextDom.text();
		}
		if(selectText!=""){
			if(selectText.indexOf(selected_text)>=0){//Determine whether it has been checked
				selectText=selectText.replace(selected_text,"").replace(",,",",");//替换掉
				selectVal=selectVal.replace(selected_val,"").replace(",,",",");//替换掉
				//Check if the last character is a comma
				if(selectText.charAt(selectText.length - 1)==","){
					// remove trailing comma
					selectText=selectText.substring(0,selectText.length - 1);
					selectVal=selectVal.substring(0,selectVal.length - 1);
				}
			}else{
				selectText+=","+selected_text;
				selectVal+=","+selected_val;
			}
		}else{
			selectText=selected_text;
			selectVal=selected_val;
		}
		$selectTextDom.text(selectText);
		$selectValDom.val(selectVal);
		if(selectText==""){
			$selectTextDom.text("Please select");
			$selectTextDom[0].setAttribute("data-is-select","false");
		}else{
			$selectTextDom[0].setAttribute("data-is-select","true");
		}
	})

Realize the effect:




 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325538427&siteId=291194637