解決方法:默認(rèn)不為文本域添加disabled和readonly等屬性,在其獲取焦點(diǎn)時(shí)添加相應(yīng)屬性,失去焦點(diǎn)時(shí)再移除。這樣就不影響jquery validation的校驗(yàn)了。
之所以默認(rèn)不添加,是因?yàn)槿绻脩舨贿M(jìn)行點(diǎn)擊操作,jQuery validation還是不進(jìn)行校驗(yàn)的。
如下:
<div class="form-group">
<label><span class="red">*</span>家庭月綜合收入(自動(dòng)計(jì)算)</label>
<input type="number" class="form-control" name="incomefamily" readonly_="readonly" autocomplete="off" placeholder="">
</div>
注意,我這里默認(rèn)為文本域添加了自定義的readonly_屬性(注意后面有下劃線),這樣方便使用jQuery選擇器批量選擇。
然后為其添加事件:
$("input").each(function(){
if($(this).attr("readonly_")=="readonly"){
$(this).on("focusin", function() {
$(this).prop('readonly', true);
});
$(this).on("focusout", function() {
$(this).prop('readonly', false);
});
};
});
$("textarea").each(function(){
if($(this).attr("readonly_")=="readonly"){
$(this).on("focusin", function() {
$(this).prop('readonly', true);
});
$(this).on("focusout", function() {
$(this).prop('readonly', false);
});
};
});