react 监听 移动端 手机键盘 enter 事件

处理方法:

(1)html书写

form标签中去掉action参数,定义onSubmit方法,如下所示:

/**
 * 搜索框 组件
 */
import React,{PureComponent} from 'react'
import Styles from './index.less'
import buttonimg from '../../assets/search_icon.png'

class SearchBar extends PureComponent{
  state = {
    searchkey: '' // 输入框的值
  };

  // 获取输入框的值
  getSearchData(val){
    this.setState({
      searchkey: val
    });
  }

  render(){
    const {text} = this.props;
    const bg = {
      backgroundImage: `url(${buttonimg})`,     
    }

    return (
      <div className={Styles.custom_search}>
        <form onSubmit={(e) => this.props.getSearchTxt(e,this.state.searchkey)} className={Styles.form}>
          <input
            type="search"
            className={Styles.custom_search_input}
            placeholder={text}
            value={this.state.searchkey}
            onChange={(e) => this.getSearchData(e.target.value)}
          />
          <button className={Styles.custom_search_button} style={bg}>搜索</button>
        </form>
      </div>
    )
  }
}

export default SearchBar;

(2)事件处理

关键的是阻止掉页面默认提交:

// 获取搜索框输入内容
getSearchTxt(e,val){
  e.preventDefault();//阻止页面提交刷新
  // 请求安排人员数据
  this.props.dispatch({
    type:'getWorkArrangePersonsList',
    param: val
  })
}

.

转载于:https://www.cnblogs.com/crazycode2/p/9292729.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值