gpt4 book ai didi

javascript - react : Perform different operation depending on select option dropdown

转载 作者:行者123 更新时间:2023-11-30 13:59:21 28 4
gpt4 key购买 nike

我在 React 中有以下形式。如果选择标签中的选项是参与,我想做的是发送一个 POST 请求。

<form action={this.props.action} method="POST" onSubmit={this.handleSubmit}>
<div className="ui action input">
<input type="text" placeholder="Enter code" name="code"/>
<select className="ui compact selection dropdown">
<option value="participate">Participate!</option>
<option value="check_status">Check status</option>
</select>
<button>
Submit
</button>
</div>
</form>

这是我的 handleSubmit 函数:

handleSubmit = (event) => {
event.preventDefault();

// HERE I want to check if the option as Participate, and then to the following code:

// Sudo code here
// IF STATEMENT event.target.value.option === 'participate'

const data = {id: this.state.code, is_winner: true};

fetch('/api/add_user', {
method: 'POST',
headers: {
'Content-type': 'application/json',
},
body: JSON.stringify(data),
})
.then(res => res.json());

// ElSE
// DO NOT SOMETHING ELSE (NOT POST)
};

最佳答案

要以“React”方式执行此操作,您需要捕获选择框的 onChange 事件:

<select onChange={e=>this.setState({selectedOption:e.target.value})} className="ui compact selection dropdown">

然后你的条件检查将是这样的:

if (this.state.selectedOption === 'participate') {...

关于javascript - react : Perform different operation depending on select option dropdown,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56635729/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com