[英]OnClick Function does not work on Chrome?
I am trying to use onClick
function on react.js HTML select option and it works perfectly on Firefox but not on Chrome. I am trying to use onClick
function on react.js HTML select option and it works perfectly on Firefox but not on Chrome. How can I make it work in Chrome?我怎样才能让它在 Chrome 中工作? Here is my code so far:到目前为止,这是我的代码:
import React, { Component } from "react";
import DateRangePicker from "react-daterange-picker";
import "react-daterange-picker/dist/css/react-calendar.css";
import originalMoment from "moment";
export class Filter extends Component {
constructor(props, context) {
super(props, context);
this.state = {
isOpen: false,};
}
onToggle = () => {
this.setState({ isOpen: !this.state.isOpen });
};
render() {
return (
<div className="filter_range">
<select
class="form-control donn"
name="today"
>
<option selected disabled hidden>
Choose{" "}
</option>
<option value="today">Today</option>
<option value="yesturday">Yesterday</option>
<option>Last Week</option>
<option value="month">Last Month</option>
<option>Last Quarter</option>
<option value="year">Last Year</option>
<option value="">Overall</option>
<option value="" onClick={this.onToggle}>
Custom
</option>
</select>
{this.state.isOpen && (
<DateRangePicker
value={this.props.value}
onSelect={this.props.change}
singleDateRange={true}
isOpen={false}
maximumDate={new Date()}
closeCalendar={true}
numberOfCalendars={2}
showLegend={true}
locale={originalMoment().locale()}
/>
)}
</div>
);
}
}
export default Filter;
Try to use onChange
instead of onClick
for select element.尝试对 select 元素使用onChange
而不是onClick
。
<select class="form-control donn" name="today" onChange={handleChange}>
Just add value to your custom option and check for it in the if statement只需为您的自定义选项添加价值并在 if 语句中检查它
<option value="custom">
Custom
</option>
export class Filter extends Component {
constructor(props, context) {
super(props, context);
this.state = {
isOpen: false,
};
}
handleChange = (event) => {
if (event.target.value === "custom") {
this.setState({ isOpen: !this.state.isOpen });
}
};
render() {
return (
<div className="filter_range">
<select class="form-control donn" name="today" onChange={handleChange}>
<option selected disabled hidden>
Choose{" "}
</option>
<option value="today">Today</option>
<option value="yesturday">Yesterday</option>
<option>Last Week</option>
<option value="month">Last Month</option>
<option>Last Quarter</option>
<option value="year">Last Year</option>
<option value="">Overall</option>
<option value="custom">
Custom
</option>
</select>
{this.state.isOpen && (
<DateRangePicker
value={this.props.value}
onSelect={this.props.change}
singleDateRange={true}
isOpen={false}
maximumDate={new Date()}
closeCalendar={true}
numberOfCalendars={2}
showLegend={true}
locale={originalMoment().locale()}
/>
)}
</div>
);
}
}
export default Filter;
onClick
- Unnecessary选项onClick
- 不需要You can put onChange
in the select
tag您可以在select
标签中添加onChange
The select onChange trigerd when option is clicked (changed).单击(更改)选项时的 select onChange 触发。
You can have a child component that only renders the option
tag.您可以有一个仅呈现option
标签的子组件。 You actually don't need to add an event handler to the option
tag.您实际上不需要将事件处理程序添加到option
标记。 The select onChange
event get called automatically once an option
tag is clicked (passing it's value with it).一旦单击option
标签(传递它的值),就会自动调用select onChange
事件。
See the example here: https://codepen.io/gaearon/pen/JbbEzX?editors=0010请参阅此处的示例: https://codepen.io/gaearon/pen/JbbEzX?editors=0010
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.