全空格问题优化

This commit is contained in:
tqzhang 2025-01-21 10:12:15 +08:00
parent 0366391848
commit c2114f9122
1 changed files with 9 additions and 10 deletions

View File

@ -83,19 +83,18 @@ Vue.directive('no-whitespace', {
const input = el.querySelector('input') || el.querySelector('textarea');
if (!input) return;
input.addEventListener('input', (event) => {
const handleInput = (event) => {
const value = event.target.value.trim();
if (value === '') {
if (value === '' || value.replace(/\s/g, '') === '') {
//设置event.target.value的值为undefined为了触发v-model的更新
input.removeEventListener('input', handleInput);
event.target.value = '';
input.dispatchEvent(new Event('input'));
input.addEventListener('input', handleInput);
}
});
input.addEventListener('blur', (event) => {
const value = event.target.value.trim();
if (value === '') {
event.target.value = '';
}
});
};
input.addEventListener('input', handleInput);
input.addEventListener('blur', handleInput);
}
});