共计 1320 个字符,预计需要花费 4 分钟才能阅读完成。
HTML:
地市 / 站点:
// 搜索框
// 选择的框
JS:
$(function () {
// 获取城市的接口方法
getUnitTree()
if ($('.cityName').val() == '') {$('.delectText').css('display', 'none')
}
// 点击选 { 择城市时,先隐藏省级市级选择块
$('.citySelect').on('click', function () {$('.dropCity').css('display', "none");
$('.dropDown').toggle();
// getUnitTree()dropProv
// 点击省份时,选择省会城市
$('.dropProvLi').off("click").on('click', function () {cityName = $(this).context.innerText
unId = $(this).attr('id')
aId = ''$(this).addClass('cityActive').siblings().removeClass('cityActive')
$('.dropCity').css('display', 'block')
if (unId != '') {
// 通过城市里面的点击获取的 id 来获取县级或区
getSite(unId)
} else {getSite(unitID)
}
$('.cityName').text(cityName);
$('.dropDown').css("display", "none");
});
// 给省级列表添加 mouseover 事件
$('.dropProvLi').on('mouseover', function () {$('.dropCityUl').empty();
cityName = $(this).context.innerText
unId = $(this).attr('id')
aId = ''$(this).addClass('cityActive').siblings().removeClass('cityActive')
$('.dropCity').css('display', 'block')
if (unId != '') {
通过城市里面鼠标经过获取的 id 来获取县级或区
getSite(unId)
} else {getSite(unitID)
}
});
});
// 删除并清空已经选中的值
$('.delectText').click(function () {$('.cityName').text('');
cityName = ''siteName =''
unId = ''aId =''
$('.delectText').css('display', 'none')
event.stopPropagation()})
})
$(document).click(function (e) {var $target = $(e.target);
// 点击地市框以外的地方隐藏盒子
if (!$target.is('#search *') && !$target.is('.dropDown')) {$('.dropDown').hide();}
});
原文地址: Jquery 实现二级下拉选择框
正文完