我有一個文本框,當用戶輸入前三個字符時,它將下拉一列自動完成選項以填充文本框。點擊其中一個自動填充選項後,我希望文本框自行清除。下面是該元素看起來像現在:清除沒有Jquery或硬編碼元素的文本框
<label
className="searchSecondary_userFilterFacet"
hidden={!this.state.isExpanded}
>
<div
className="data-uk-autocomplete"
id="userFilterTypeahead"
ref={elem => (this.userFilterTypeahead = elem)}
>
<input
id="textInput_userSearch"
onKeyUp={this._handleNewUser}
placeholder="Search Users"
type="text"
ref={input => (this.userInput = input)}
/>
</div>
{userModifiers}
</label>
我使用的反應。這裏是我的代碼以清除文本框目前的樣子:
componentDidMount() {
let element = ReactDOM.findDOMNode(this.userFilterTypeahead);
//bind selection event for user filter autocompletion uikit
$('#userFilterTypeahead').on(
'selectitem.uk.autocomplete',
function(event, data, acobject) {
$('#textInput_userSearch').val('');
this._editUsersBuffer(data.value, true, true);
}.bind(this)
);
}
首先我需要刪除Jquery的線,實際上是清除複選框。我發現用this.userInput.value=""
代替它沒有效果,event.target.value=""
也沒有效果。其次,我想刪除Jquery行$('#userFilterTypeahead').on( 'selectitem.uk.autocomplete',...
,但在selectitem.uk.autocomplete
事件發生時,我仍然需要觸發我的文本清除。所以,我想替換這個Jquery,並且對清除單個文本框所帶來的複雜性感到困惑。
'this.userInput.value ='''應該已經工作了。你可以console.log this.userInput並確保它存在並且不是undefined嗎?您可能需要使用ref回調,而不是執行內聯ref。 –
@ChaseDeAnda我記錄了'this.userInput'並得到了' –
你確定函數被調用嗎?你的代碼看起來很好。 –