问题:bootstrap 官网中只演示了css样式,没有给js,无法获取值
思路:多次尝试,发现switch的val值可以获取到,于是通过js监控点击事件,给switch赋值实现该功能。若为新增则值为空,将value赋值0;若为新增,判断有效无效,若有效模拟点击事件切换样式。
jsp代码:
<div class="form-group">
<label class="col-sm-3 control-label no-padding-right" for="isauto">是否有效</label>
<div class="col-sm-3">
<label>
<input name="efective" id="effective" class="ace ace-switch ace-switch-6" type="checkbox" value="${goodsTypeVo.effective}">
<span class="lbl"></span>
</label>
</div>
<div class="help-block"></div>
</div>
js代码:
jQuery(function($) {
if( $("#effective").val()==null){
$("#effective").val("0")
}else if($("#effective").val()==1){
$("#effective").click()
}
console.log('$("#effective").val()'+($("#effective").val()));
$("#effective").on('click', function(){
clickSwitch()
});
function clickSwitch() {
if ($("#effective").val()=="1") {
$("#effective").val("0")
console.log("由ON切换到OFF:"+$("#effective").val());
} else {
$("#effective").val("1")
console.log("在OFF切换到ON:"+$("#effective").val());
}
};
})