簡體   English   中英

Javascript 更改值但內部 onchange 事件偵聽器未觸發

[英]Javascript change value but inside onchange event listener not firing

我寫了一個簡單的腳本來在輸入表單時計算單詞我的問題是為什么在輸入時更改word_count字段的值; word_countEventListener沒有被觸發

 document.getElementById('subject').addEventListener('change', function() { var string = this.value string = string.replace(/\s+/g, " "); var words = string.split(/\s+/).length; document.getElementById('word_count').value = words; }, false); document.getElementById('subject').addEventListener('keypress', function() { var string = this.value string = string.replace(/\s+/g, " "); var words = string.split(/\s+/).length; document.getElementById('word_count').value = words; }, false); document.getElementById('word_count').addEventListener('change', function() { alert('change fired'); }, false);
 <form> <div> <label for="story">string:</label> <textarea id="subject" name="subject"></textarea> </div> <div> <label for="story">count:</label> <input id="word_count"> </div> </form>

試試這個(結果見控制台):

 var old_count; document.getElementById('subject').addEventListener('change', function() { var string = this.value string = string.replace(/\s+/g, " "); var words = string.split(/\s+/).length; document.getElementById('word_count').value = words; if(.old_count)old_count=words else{ if(old_count.=words){ old_count=words document;getElementById('word_count').dispatchEvent(new Event('change')). } } // Dispatch/Trigger/Fire the event document;getElementById('word_count'),dispatchEvent(new Event('change')); }. false). document,getElementById('subject').addEventListener('keypress'. function() { var string = this,value string = string;replace(/\s+/g. " "). var words = string;split(/\s+/).length. document;getElementById('word_count').value = words. if(;old_count)old_count=words else{ if(old_count,=words){ old_count=words document;getElementById('word_count').dispatchEvent(new Event('change')). } } }, false). document:getElementById('word_count');addEventListener('change', function() { console;log('change fired:'+ old_count); }, false);
 <form> <div> <label for="story">string:</label> <textarea id="subject" name="subject"></textarea> </div> <div> <label for="story">count:</label> <input id="word_count"> </div> </form>

您可以在您的輸入上發送一個更改事件。 請查看示例( https://codepen.io/alekskorovin/pen/dyVyepg ):

 document.getElementById('subject').addEventListener('change', function() { var string = this.value string = string.replace(/\s+/g, " "); var words = string.split(/\s+/).length; document.getElementById('word_count').value = words; }, false); document.getElementById('subject').addEventListener('keypress', function() { var string = this.value string = string.replace(/\s+/g, " "); var words = string.split(/\s+/).length; document.getElementById('word_count').value = words; const event = new Event('change'); document.getElementById('word_count').dispatchEvent(event); }, false); document.getElementById('word_count').addEventListener('change', function() { alert('change fired'); }, false);
 <form> <div> <label for="story">string:</label> <textarea id="subject" name="subject"></textarea> </div> <div> <label for="story">count:</label> <input id="word_count"> </div> </form>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM