jda*_*ing 105 checkbox onchange reactjs
TLDR:使用defaultChecked而不是checked,在这里工作jsbin http://jsbin.com/mecimayawe/1/edit?js,output
尝试设置一个简单的复选框,在选中时会勾选其标签文本.出于某种原因,当我使用该组件时,handleChange不会被触发.谁能解释我做错了什么?
var CrossoutCheckbox = React.createClass({
getInitialState: function () {
return {
complete: (!!this.props.complete) || false
};
},
handleChange: function(){
console.log('handleChange', this.refs.complete.checked); // Never gets logged
this.setState({
complete: this.refs.complete.checked
});
},
render: function(){
var labelStyle={
'text-decoration': this.state.complete?'line-through':''
};
return (
<span>
<label style={labelStyle}>
<input
type="checkbox"
checked={this.state.complete}
ref="complete"
onChange={this.handleChange}
/>
{this.props.text}
</label>
</span>
);
}
});
Run Code Online (Sandbox Code Playgroud)
用法:
React.renderComponent(CrossoutCheckbox({text: "Text Text", complete: false}), mountNode);
Run Code Online (Sandbox Code Playgroud)
解:
使用checked不会让底层值发生变化(显然),因此不会调用onChange处理程序.切换到defaultChecked似乎解决了这个问题:
var CrossoutCheckbox = React.createClass({
getInitialState: function () {
return {
complete: (!!this.props.complete) || false
};
},
handleChange: function(){
this.setState({
complete: !this.state.complete
});
},
render: function(){
var labelStyle={
'text-decoration': this.state.complete?'line-through':''
};
return (
<span>
<label style={labelStyle}>
<input
type="checkbox"
defaultChecked={this.state.complete}
ref="complete"
onChange={this.handleChange}
/>
{this.props.text}
</label>
</span>
);
}
});
Run Code Online (Sandbox Code Playgroud)
zby*_*yte 168
要获得复选框的选中状态,路径将为:
this.refs.complete.state.checked
Run Code Online (Sandbox Code Playgroud)
另一种方法是从传递给handleChange
方法的事件中获取它:
event.target.checked
Run Code Online (Sandbox Code Playgroud)
Lin*_*Lin 18
在这种情况下最好不要使用refs.使用:
<input
type="checkbox"
checked={this.state.active}
onClick={this.handleClick}
/>
Run Code Online (Sandbox Code Playgroud)
有一些选择:
checked
VS defaultChecked
前者将响应状态变化和点击.后者会忽略状态变化.
onClick
VS onChange
前者总是会触发点击.如果元素checked
上存在属性,则后者不会触发点击input
.
在场景中,您不希望在输入DOM上使用onChange处理程序,您可以使用该onClick
属性作为替代.的defaultChecked
,条件可离开V16 IINM固定状态.
class CrossOutCheckbox extends Component {
constructor(init){
super(init);
this.handleChange = this.handleChange.bind(this);
}
handleChange({target}){
if (target.checked){
target.removeAttribute('checked');
target.parentNode.style.textDecoration = "";
} else {
target.setAttribute('checked', true);
target.parentNode.style.textDecoration = "line-through";
}
}
render(){
return (
<span>
<label style={{textDecoration: this.props.complete?"line-through":""}}>
<input type="checkbox"
onClick={this.handleChange}
defaultChecked={this.props.complete}
/>
</label>
{this.props.text}
</span>
)
}
}
Run Code Online (Sandbox Code Playgroud)
我希望这可以帮助将来的某个人.
如果有人正在寻找通用事件处理程序,可以或多或少地使用以下代码(假设为每个输入设置了name属性):
this.handleInputChange = (e) => {
item[e.target.name] = e.target.type === "checkbox" ? e.target.checked : e.target.value;
}
Run Code Online (Sandbox Code Playgroud)
如果您具有如下所示的handleChange
函数:
handleChange = (e) => {
this.setState({
[e.target.name]: e.target.value,
});
}
Run Code Online (Sandbox Code Playgroud)
您可以创建一个自定义onChange
函数,使其像文本输入一样:
<input
type="checkbox"
name="check"
checked={this.state.check}
onChange={(e) => {
this.handleChange({
target: {
name: e.target.name,
value: e.target.checked,
},
});
}}
/>
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
136529 次 |
最近记录: |