我正在創(chuàng)建一個帶有反應(yīng)語義 UI 的文本輸入,當單擊按鈕時將輸入文本。一切都運行良好,只是此操作不會觸發(fā)onchange事件,因此不會更新狀態(tài)。這是我將文本附加到輸入的函數(shù)const putTextAtCursor = (text, inputID) => { let inputElement = document.getElementById(inputID); console.log({ inputElement, inputID }); let cursorPosition = 0; if (document.selection) { inputElement.focus(); let selectionRange = document.selection.createRange(); selectionRange.moveStart("character", -inputElement.value.length); cursorPosition = selectionRange.text.length; } else if ( inputElement.selectionStart || inputElement.selectionStart == "0" ) { cursorPosition = inputElement.selectionStart; } let origValue = inputElement.value; let newValue = origValue.substr(0, cursorPosition) + text + origValue.substr(cursorPosition); inputElement.value = newValue; let start = inputElement.selectionStart; inputElement.selectionStart = inputElement.selectionEnd = start + text.length; let event = new UIEvent("change"); inputElement.dispatchEvent(event); inputElement.focus();};在dispatchEvent似乎沒有被解雇。完整演示在這里如何解決這個問題?
React-Semantic UI 調(diào)度事件不起作用
回首憶惘然
2021-09-17 13:47:20