【发布时间】:2018-11-01 03:00:22
【问题描述】:
情况:我有一个类有自己的方法。此类在 React 组件中实例化。
我需要什么:在类中的一个方法中,它使用 .val() 更改了输入 (this.$el) 的值,但我正在听取更改通过onChange 在 React.component 中输入。我需要将用于设置输入值的值(通过this.$el.val(value))传递给 React 组件以更改其状态。
我的尝试:我尝试将.change() 和trigger('change') 链接到val(value),但没有任何影响。
所以,当在类方法中设置时,我需要能够访问我在 React 组件中 .val(value) 中使用的值。我考虑过使用一个方法并在componentWillUpdate 上调用该方法,但是组件没有更新,因为通过val() 设置输入值不会触发更改。
有什么想法吗?
组件代码:
// Create a ref to manage blur/focus state on the search input
this.inputRef = React.createRef()
// Setup initial state
this.state = {
supersearchResults: [],
value: this.props.initialValue || ''
}
this.onInputChange = this.onInputChange.bind(this)
tag('input', {
autoComplete: 'off',
className: blockElement('search-input'),
onChange: this.onInputChange,
placeholder: 'Find people, collections and pages',
ref: this.inputRef,
type: 'text',
value: this.state.value
})
类代码:
this = 班级
this.$el = 输入
// What is happening:
// A user types in an input, suggestions display in a list, when you
// select a suggestion, it calls the below to change the input value
this.$el.val(complete)
this.$el.blur()
this.hide()
【问题讨论】:
-
能多加点代码理解吗?
-
添加了更多。应该够了
标签: javascript jquery reactjs class data-binding