首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >ReactJS:触发事件onBlur

ReactJS:触发事件onBlur
EN

Stack Overflow用户
提问于 2016-06-18 19:44:11
回答 1查看 1.6K关注 0票数 1

我有输入域的子组件,它正在检查它的验证onBlur事件。

子组件用法:

代码语言:javascript
复制
<TextInput
    id={'lastName'}
    label={'Last name'}
    required={true}
    minChars={3}
    maxChars={25}
/>

下级组件编码:

代码语言:javascript
复制
onBlur(event) {
    // logic here
}

render() {
    let props = this.props;

    return (
        <div>
            <div className="form-group">
                <label htmlFor={props.id}>{props.label}</label>
                <input
                    id={props.id}
                    type={props.type}
                    className="form-control"
                    onBlur={this.onBlur.bind(this)}
                />
                <p className="text-danger">{this.error}</p>
            </div>
        </div>
    );
}

这个很好用。

当用户从父组件提交表单时,我希望在所有输入中触发onBlur。我怎么才能做到这一点呢?

EN

回答 1

Stack Overflow用户

发布于 2016-06-18 20:03:35

在父组件中:

代码语言:javascript
复制
_handleBlur (eventData) {}

render () {
    const handleBlur = this._handleBlur.bind(this);

    return (
        <form>
            <ChildComponent onBlur={handleBlur} {...moreProps} />
            <ChildComponent onBlur={handleBlur} {...moreProps} />
            <ChildComponent onBlur={handleBlur} {...moreProps} />
            <button type="submit" onClick={handleBlur}>Submit</button>
        </form>
    );
}

在子组件中:

代码语言:javascript
复制
render () {
    const props = this.props;

    return (
        <div>
            <div className="form-group">
                <label htmlFor={props.id}>{props.label}</label>
                <input
                    id={props.id}
                    type={props.type}
                    className="form-control"
                    onBlur={props.onBlur}
                />
                <p className="text-danger">{this.error}</p>
            </div>
        </div>
    );
}

基于eventData或其他参数,您可以定义需要模糊的字段。我不知道在模糊中到底需要发生什么。在某些情况下,将其拆分为handleBlur和handleBlurAll方法可能会更好。希望这能有所帮助

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/37896537

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档