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>
            
        )
    }
}
相关推荐
崔庆才丨静觅1 小时前
hCaptcha 验证码图像识别 API 对接教程
前端
lly2024061 小时前
Bootstrap 警告框
开发语言
2601_949146532 小时前
C语言语音通知接口接入教程:如何使用C语言直接调用语音预警API
c语言·开发语言
曹牧2 小时前
Spring Boot:如何测试Java Controller中的POST请求?
java·开发语言
passerby60612 小时前
完成前端时间处理的另一块版图
前端·github·web components
KYGALYX2 小时前
服务异步通信
开发语言·后端·微服务·ruby
掘了2 小时前
「2025 年终总结」在所有失去的人中,我最怀念我自己
前端·后端·年终总结
zmzb01032 小时前
C++课后习题训练记录Day98
开发语言·c++
崔庆才丨静觅2 小时前
实用免费的 Short URL 短链接 API 对接说明
前端
崔庆才丨静觅3 小时前
5分钟快速搭建 AI 平台并用它赚钱!
前端