初始化
This commit is contained in:
37
src/directive/input/phone.js
Normal file
37
src/directive/input/phone.js
Normal file
@@ -0,0 +1,37 @@
|
||||
export default {
|
||||
inserted(el, bind, vnode, prevVnode) {
|
||||
const input = el.nodeName.toLowerCase() === 'input' ? el : el.getElementsByTagName('input')[0];
|
||||
// 改变值过后,需要主动触发input时间,组件绑定的值才会改变
|
||||
let triggerBySelf = false;
|
||||
if (input) {
|
||||
input.addEventListener('input', () => {
|
||||
if (triggerBySelf) {
|
||||
triggerBySelf = false;
|
||||
return;
|
||||
}
|
||||
const val = input.value.replace(/[^0-9]/gi, '');
|
||||
if (val === '') {
|
||||
input.value = '';
|
||||
return;
|
||||
}
|
||||
if (bind.expression) {
|
||||
try {
|
||||
const option = bind.value;
|
||||
if (typeof option === 'object') {
|
||||
console.log(option);
|
||||
|
||||
// 改变值过后,需要主动触发input时间,组件绑定的值才会改变
|
||||
const ev = new Event('input', { bubbles: true });
|
||||
input.value = val;
|
||||
triggerBySelf = true;
|
||||
input.dispatchEvent(ev);
|
||||
}
|
||||
} catch (err) {
|
||||
input.value = val;
|
||||
console.error(err);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
};
|
||||
41
src/directive/input/trim.js
Normal file
41
src/directive/input/trim.js
Normal file
@@ -0,0 +1,41 @@
|
||||
/*
|
||||
* @Author: riverQiu
|
||||
* @Date: 2022-11-04
|
||||
* @LastEditors: riverQiu
|
||||
* @LastEditTime: 2022-11-04
|
||||
* @Description: v-jtrim 去除所有空格
|
||||
*/
|
||||
|
||||
export default {
|
||||
inserted(el, bind, vnode, prevVnode) {
|
||||
const input =
|
||||
el.nodeName.toLowerCase() === "input"
|
||||
? el
|
||||
: el.getElementsByTagName("input")[0] ? el.getElementsByTagName("input")[0] : el.getElementsByTagName("textarea")[0];
|
||||
// 改变值过后,需要主动触发input时间,组件绑定的值才会改变
|
||||
let triggerBySelf = false;
|
||||
if (input) {
|
||||
input.addEventListener("input", () => {
|
||||
if (triggerBySelf) {
|
||||
triggerBySelf = false;
|
||||
return;
|
||||
}
|
||||
let val = input.value.replace(/[ ]/g, "");
|
||||
if (val === "") {
|
||||
input.value = "";
|
||||
return;
|
||||
}
|
||||
try {
|
||||
// 改变值过后,需要主动触发input时间,组件绑定的值才会改变
|
||||
const ev = new Event("input", { bubbles: true });
|
||||
input.value = val;
|
||||
triggerBySelf = true;
|
||||
input.dispatchEvent(ev);
|
||||
} catch (err) {
|
||||
input.value = val;
|
||||
console.error(err);
|
||||
}
|
||||
});
|
||||
}
|
||||
},
|
||||
};
|
||||
Reference in New Issue
Block a user