react-antd组件 input输入框: 实现按回车搜索

目录

法1:

法2:


法1:

在Input组件上绑定onKeyUp方法

javascript 复制代码
import { Input, message } from 'antd';

class App extends React.Component{

    handeleSousuo = () => {
        this.props.form.validateFields((error, values) => {
            if(!error){
                axios.post().then(res => {
                    if(res.code === 200){
                        
                    } else {
                        message.error('出错啦,刷新试试!')
                    }
                })
            }
        })
    }

    inputKeyUp=(e)=>{
        console.log(e, 'e')
        if(e.keyCode === 13){
            this.handeleSousuo(); //调搜索接口 
        }
    }


    render(){
        return(
            <div>
                <Input onKeyUp={this.inputKeyUp} />
            </div>
            
        )
    }
}

法2:

antd组件里搜索框

javascript 复制代码
import { Input } from 'antd';

const { Search } = Input ;

class App extends React.Component{

    search = () => {
        this.props.form.validateFields((error, values) => {
            if(!error){
                axios.post().then(res => {
                    if(res.code === 200){
                        
                    } else {
                        message.error('出错啦,刷新试试!')
                    }
                })
            }
        })
    }

   
    render(){
        return(
            <div>
                <Search onSearch={() => this.search()} />
            </div>
            
        )
    }
}
相关推荐
ji_shuke2 分钟前
Vue 3 使用 History 哨兵和 popstate 拦截浏览器返回
前端·javascript·vue.js·history·哨兵·popstate
l1t27 分钟前
DeepSeek总结的DuckDB 如何更快地运行递归 CTE
java·开发语言·数据库·mysql·duckdb
HAPPY酷27 分钟前
python的对象和方法
开发语言·python
橙露32 分钟前
移动端客户端原生开发语言
开发语言
Demon--hx37 分钟前
设计不能被继承的类
开发语言·c++
名字还没想好☜38 分钟前
Prometheus 告警实战:写 alerting rules、用 Alertmanager 做路由分组与抑制
运维·前端·javascript·docker·kubernetes·prometheus
郭邯1 小时前
从零撸了一个 HTML 实体编解码工具,顺便聊聊我和 AI 结对编程的日常
前端
兔兔兔兔11 小时前
记录C++ 13
开发语言·c++·算法
樊小肆1 小时前
DeepSeeker-Code源码导读12-Hooks四引擎
前端·人工智能·agent
码行山野赴时序归途1 小时前
从暴力到最优:三道 C 语言入门题的解法思路
c语言·开发语言·数据结构·算法·leetcode·排序算法