做搜索功能的時候,經(jīng)常遇到輸入框檢查的需求,最常見的是即時搜索,今天好好小結(jié)一下。
即時搜索的方案:
(1)change事件 觸發(fā)事件必須滿足兩個條件:
propertychange,只要當(dāng)前對象屬性發(fā)生改變。
下面我們用jquery 來實(shí)現(xiàn)input 等同于placeholder 這個屬性的效果
<div class="enterprise-list">
<label>銀行卡號:</label>
<input type="text" placeholder="請輸入16或19位銀行卡號" class="enterprise-inp" id="cartInput">
</div>
<script>
$(function () {
$("#cartInput").bind('input propertychange',function () {
var text = $("#cartInput").val();
text = text.replace(/[^\d]/g,'');
console.log(text)
})
})
</script>
<input type="text" v-model="bankcard" class="enterprise-inp" v-on:input="cartInput">
cartInput:function () {
this.bankcard=this.bankcard.replace(/[^\d]/g,'');
console.log(this.bankcard)
},