[英]How can i remove the repeating words in my html input field
<input id="btn-input" type="text" class="form-control input-sm" placeholder="Type your message here..." name="subject" pattern=".+?(?:[\s'].+?){2,}" autofocus required title="" />
I have a problem with this, it is inserting when i insert repeating words like college college college and i also wanto to have a question format it is possible?我有这个问题,当我插入像大学学院这样的重复单词时会插入,我也想有一个问题格式,这可能吗? also open for java script suggestions even i already have
即使我已经有了,也可以接受 java 脚本建议
This works pretty good (when user pastes, inserts anything):这很好用(当用户粘贴时,插入任何东西):
Code Snippet:代码片段:
<!DOCTYPE HTML> <html> <body> <input id="btn-input" type="text" class="form-control input-sm" placeholder="Type your message here..." name="subject" pattern=".+?(?:[\\s'].+?){2,}" autofocus required title="" /> <button id="toggleduplicates">Toggle allowing duplicates</button> <script> // I added this to allow duplicates let allowDuplicates = false; var element = document.getElementById("btn-input"), togglebtn = document.getElementById("toggleduplicates"); function handler(e){ if(!allowDuplicates){ var userInput = element.value, allWords = userInput.split(" "), searchedWords = []; // Search thru all words (Indicating words by splitting the input in spaces, as done in the "allWords" variable) for(i = 0; i < allWords.length; i++){ // If the current word (allWords[i]) already is in the searchedWords array if(searchedWords.includes(allWords[i])) element.value = element.value.replace(allWords[i] + " ", ""); // Adds the word to the list of used words searchedWords.push(allWords[i]); }}} // Execute the function "handler" when the input field is updated element.addEventListener("input", handler); function toggleHandler(){ // Setting the allow duplicates boolean to the value that is not currently (true -> false / false -> true) allowDuplicates = !allowDuplicates; // Updates the input, in case it would include duplicates handler(); alert("You may now " + (!allowDuplicates ? "NOT ":"") + "use duplicate words."); } // Execute the function "toggleHandler" when the button is clicked togglebtn.addEventListener("click", toggleHandler); </script> </body> </html>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.