在react/umin antd开发时,有时会遇到同一个页面有多个button,可以为每一个button写一个click事件。也可以只要一个click事件,在事件处理程序中对各个button进行识别。核心是利用事件参数来区别每一个button.
示例如下
app.tsx
import React from 'react';
import {
Button,
Space,
} from 'antd';
const App: React.FC = () => {
const handleButton= (e:any) =>{
//console.log(typeof e.target, String(e.target),e.target.textContent)
switch(e.target.textContent) {
case "submit1" :
console.log("单击submit1")
break;
case "submit2" :
console.log("单击submit2")
break;
case "submit3" :
console.log("单击submit3")
break;
}
}
return (
<>
<Space>
<Button id="b01" name="nb01" htmlType="button" onClick={handleButton}>submit1</Button>
<Button id="b02" name="nb02" htmlType="button" onClick={handleButton}>submit2</Button>
<Button id="b03" name="nb03" htmlType="button" onClick={handleButton}>submit3</Button>
</Space>
</>
)
};
export default App;