在这种情况下,我们要过滤输入中的单词:


<input type="text" class="form-control" placeholder="Write something..." v-model="todoInput"">





这些是我们要从输入中替换的禁止词


  “禁止”,“苹果”,“香蕉”,

最佳答案

这是我们的Vue实例。监视者将禁止的单词替换为星号(*)



const app = new Vue({
    el: '#app',
    data: function(){
    	return {
    		todoInput : '',
    	}
    },
    watch: {
    	todoInput: function(){
    		var banned = ["banned", "apple", "banana"]
    		for (var i = 0; i < banned.length; i++) {
    			if (this.todoInput.includes(banned[i])) {
	    			this.todoInput = this.todoInput.replace(banned[i], "*".repeat(banned[i].length)) //sets the input to * times the length of the banned word
	    		}
    		}
    	}
    }
});

<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.4.2/vue.min.js"></script>

<div id="app">

    <input type="text" class="form-control"
            placeholder="Write something..."
            v-model="todoInput">

</div>

关于javascript - 过滤并替换被禁止的单词Vue js,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/45257882/

10-12 00:21