前言
用于记录开发中常用到的,快捷开发
简单实例
<select>
<option value="volvo">Volvo</option>
<option value="saab">Saab</option>
<option value="mercedes">Mercedes</option>
<option value="audi">Audi</option>
</select>
开发实例
<div class="form-group"><div class="row"><div class="col-sm-6">
<label class="col-sm-3 control-label">医院</label>
<div class="col-sm-9">
<select id="hospitalCode" name="hospitalCode" data-placeholder="选择医院..." class="chosen-select" >
<option value="" >请选择</option>
[#list hospitals as hospital]
<option value="${hospital.code}" >${hospital.code}-${hospital.name}</option>
[/#list]
</select>
</div>
</div></div></div>
常用事件
onchange (选择后触发)
方式一
通过 JS 直接监听 select id。
HTML
<div class="form-group"><div class="row"><div class="col-sm-6">
<label class="col-sm-3 control-label">医院</label>
<div class="col-sm-9">
<select id="hospitalCode" name="hospitalCode" data-placeholder="选择医院..." class="chosen-select" >
<option value="" >请选择</option>
[#list hospitals as hospital]
<option value="${hospital.code}" >${hospital.code}-${hospital.name}</option>
[/#list]
</select>
</div>
</div></div></div>
JS
document.getElementById("hospitalCode").onchange =function(){
const code = document.getElementById("hospitalCode").value
if (code != null) {
$.ajax({
url: "${base}/admin/hospital_group/get_hospital_grade.json",
type: "POST",
cache: false,
data: {"code":code},
success: function(data) {
if(data){
var json = $.parseJSON(data)
if (json.code === "1") {
// 赋值
$("#hospitalGrade").attr("value",json.object);
}
}
}
})
}
}
方式二
传统的方式,在 select 标签上加一个 onchange 。
HTML
<div class="form-group"><div class="row"><div class="col-sm-6">
<label class="col-sm-3 control-label">医院</label>
<div class="col-sm-9">
<select id="hospitalCode" name="hospitalCode" data-placeholder="选择医院..." class="chosen-select" onchange="check()">
<option value="" >请选择</option>
[#list hospitals as hospital]
<option value="${hospital.code}" >${hospital.code}-${hospital.name}</option>
[/#list]
</select>
</div>
</div></div></div>
JS
function check(){
const code = document.getElementById("hospitalCode").value
if (code != null) {
$.ajax({
url: "${base}/admin/hospital_group/get_hospital_grade.json",
type: "POST",
cache: false,
data: {"code":code},
success: function(data) {
if(data){
var json = $.parseJSON(data)
if (json.code === "1") {
// 赋值
$("#hospitalGrade").attr("value",json.object);
}
}
}
})
}
}
总结
后续用到的我会继续写进去,如果有大佬遇到其他常用的,欢迎评论