效 果:

因為需要做一個搜索出現下拉然后點擊 自動填裝input 內容的 東西。
一開始使用lay-search 的控件去弄。 但是無法控制里面的內容。所以用了一些笨方法去弄
廢話不說了,
html部分:
<div >
<input type="text" name="HandoverCompany" id="HandoverCompany" onchange="checkInputValueLow(this)" class="layui-input" placeholder="請輸入檢查單位" style="position:absolute;z-index:2;width: 78%;border-right: 0px;" lay-verify="required" value="" onkeyup="search()" autocomplete="off">
<select type="text" id="hc_select" lay-filter="hc_select" autocomplete="off" placeholder="移交單位全稱" lay-verify="required" class="layui-select" lay-search>
</select>
</div>
css部分:
.border-left-st{
border-left:1px solid red;
}
.border-top-st{
border-top:1px solid red;
}
.border-bottom-st{
border-bottom:1px solid red;
}
.borderRed{
border:1px solid red;
}
js+layui部分:
layui.use(['form', 'layedit', 'laydate'], function(){
var form = layui.form
,layer = layui.layer
,layedit = layui.layedit
,laydate = layui.laydate;
form.on('select(hc_select)', function (data) { //選擇移交單位 賦值給input框
console.log(data);
console.log(optionValue[data.value].regAddress);
$("#HandoverCompany").val(optionValue[data.value].enterpriseName);
$("#enterpriseAddress").val(optionValue[data.value].regAddress);
$("#enterpriseId").val(optionValue[data.value].enterpriseId);
$("#hc_select").next().find("dl").css({ "display": "none" });
form.render();
});
window.search = function () {
var value = $("#HandoverCompany").val();
$("#hc_select").val(value);
console.log(value)
form.render();
$("#hc_select").next().find("dl").css({ "display": "block" });
var dl = $("#hc_select").next().find("dl").children();
var j = -1;
for (var i = 0; i < dl.length; i++) {
if (dl[i].innerHTML.indexOf(value) <= -1) {
dl[i].style.display = "none";
j++;
}
if (j == dl.length-1) {
$("#hc_select").next().find("dl").css({ "display": "none" });
}
}
}
//一定要用延遲 不然會選取不到內容
$("#HandoverCompany").blur(function(){
setTimeout(
function(){$("#hc_select").next().find("dl").css({ "display": "none" });},100
)
});
$("#HandoverCompany").focus(function(){
$("#hc_select").next().find("dl").css({ "display": "block" });
});
});
控制顯示等部分
function addProblemCol(){
$("#problemTable").append('<tr><td onclick="cutProblemCol(this)" class="cutPotiner">移除問題</td><td><input type="text" name="problemInfo" id="problemInfo" lay-verify="problemInfo" autocomplete="off" placeholder="請輸入問題詳情" class="layui-input addProblemCount" maxlength="60" ></td</tr>')
}
function cutProblemCol(obj){
$(obj).parent().remove();
}
function checkInputValue(obj){
if($(obj).val()!='')
{
$(obj).removeClass("borderRed");
}
}
function checkInputValueLow(obj){
$(obj).removeClass("border-left-st");
$(obj).removeClass("border-bottom-st");
$(obj).removeClass("border-top-st");
$("input").each(function(index,element){
if(index ==1)
{
//$(element).attr("border","none");
}
})
}
$("#HandoverCompany").on('compositionstart',function(){
keyFlag = false;
})
$('#HandoverCompany').on('compositionend',function(){
keyFlag = true;
})
判斷輸入狀態調取后台部分
var optionValue = [];
$("#HandoverCompany").on('input',function(){
var value = $("#HandoverCompany").val();
console.log("123123123");
if(value.length>2 && keyFlag )
{
console.log(value.length)
console.log(keyFlag)
$.ajax({
type:"post",
url:"寫入自己后台的地址",
cache:false,
async:true,
data:{你的參數},
success:function(data){
$("#hc_select").empty();
optionValue=[];
$(data).each(function(index,element){
optionValue.push(element);
$("#hc_select").append('<option value='+index+'>'+element.enterpriseName+'</option>');
});
}
});
}
});
提交判斷看情況。 因為這里是使用的parent.xxx.open,彈窗在最上層 , 常規判斷 不變就是提示的部分改了
if($(enterpriseName).val()==null || $(enterpriseName).val().trim()=='')
{
parent.layer.msg("請輸入被檢查單位名稱");
//非主流寫法 不可再用... 需要確定input的位置不會變
$(inputTag).each(function(index,element){
if(index == 0)
{
$(element).addClass("border-left-st");
$(element).addClass("border-top-st");
$(element).addClass("border-bottom-st");
}
if(index==1)
{
$(element).addClass("borderRed");
}
});
return false;
}
