diff --git a/src/main.js b/src/main.js index c36d285..3a18d49 100644 --- a/src/main.js +++ b/src/main.js @@ -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); } });