gpt4 book ai didi

javascript - 在 Reactjs 中选中/取消选中属性 onChange 复选框

转载 作者:行者123 更新时间:2023-11-30 15:50:07 25 4
gpt4 key购买 nike

我的 react 组件有问题。我有像这样的自定义样式复选框 http://jsfiddle.net/fvdbcch0/

我想点击我的组件或标签来选中或取消选中输入。

目前它仅在我准确点击输入时才适用于基本样式。

如何正确更改选中状态 onChange ?

代码:

 var Tag = React.createClass({
getInitialState: function(){

return {
checked: false
};
},
componentDidMount: function() {
this.setState({
checked: false
});
},
_onChange: function(event) {
if(this.state.checked == false){
this.setState({
checked: true
});
} else {
this.setState({
checked: false
});
}
},
render: function() {
return (
<div>
<input type="checkbox" id="{this.props.id}" name="{this.props.name}" checked={this.state.checked} onChange={ this._onChange }
value={ this.props.id }/><label htmlFor="{this.props.name}">{this.props.name}</label>
</div>
);
}
});
var ReviewTag = React.createClass({
render: function() {
var tags = [{"id": 1, "name": "friendly"}, {"id": 2, "name": "comfortable"}, {"id": 3, "name": "fast"}];
const tagComps = tags.map(function(tag){
return <Tag {...tag} />;
});
return (
<div>
{tagComps}
</div>
);
}
});

React.render(<ReviewTag/>, document.body);

以及我的自定义输入的样式:

/* Base for label styling */
[type="checkbox"]:not(:checked),
[type="checkbox"]:checked,
[type="radio"]:not(:checked),
[type="radio"]:checked{
position: absolute;
left: -9999px;
}
[type="checkbox"]:not(:checked) + label,
[type="checkbox"]:checked + label,
[type="radio"]:not(:checked) + label,
[type="radio"]:checked + label{
position: relative;
padding-left: 25px;
cursor: pointer;
}

/* checkbox/radio aspect */
[type="checkbox"]:not(:checked) + label:before,
[type="checkbox"]:checked + label:before,
[type="radio"]:not(:checked) + label:before,
[type="radio"]:checked + label:before{
content: '';
position: absolute;
left:0; top: 50%;
width: 1rem; height:1rem;
border: 1px solid #aaa;
background: #fff;
box-shadow: inset 0 1px 3px rgba(0,0,0,.3);
transform: translateY(-50%);
}
[type="radio"]:not(:checked) + label:before,
[type="radio"]:checked + label:before{
border-radius:100%;
}
/* checked mark aspect */
[type="checkbox"]:checked + label:after {
content: '✓';
position: absolute;
top: 50%;
left: 0px;
font-size: 1rem;
line-height: 0.8;
color: #e71558;
transition: all .2s;
transform: translateY(-50%);
}
/* checked radio aspect */
[type="radio"]:checked + label:after {
content: '•';
position: absolute;
top: 6px;
left: 2px;
font-size: 15px;
line-height: 0.8;
color: #e71558;
transition: all .2s;
//transform: translateY(-50%);
text-align: center;
}
/* checked mark aspect changes */
[type="checkbox"]:not(:checked) + label:after,
[type="radio"]:not(:checked) + label:after{
opacity: 0;
transform: scale(0);
}
[type="checkbox"]:checked + label:after,
[type="radio"]:checked + label:after {
opacity: 1;
//transform: scale(1);
}
/* disabled checkbox/radio */
[type="checkbox"]:disabled:not(:checked) + label:before,
[type="checkbox"]:disabled:checked + label:before,
[type="radio"]:disabled:not(:checked) + label:before,
[type="radio"]:disabled:checked + label:before{
box-shadow: none;
border-color: #bbb;
background-color: #ddd;
}
[type="checkbox"]:disabled:checked + label:after,
[type="radio"]:disabled:checked + label:after{
color: #999;
}
[type="checkbox"]:disabled + label,
[type="radio"]:disabled + label{
color: #aaa;
}
/* accessibility */
[type="checkbox"]:checked:focus + label:before,
[type="checkbox"]:not(:checked):focus + label:before,
[type="radio"]:checked:focus + label:before,
[type="radio"]:not(:checked):focus + label:before{
border: 1px dotted blue;
}

/* hover style just for information */
label:hover:before {
border: 1px solid #4778d9!important;
}

非常感谢您的帮助。

最佳答案

您的复选框缺少一些使其工作所需的 Prop 。您需要能够使用 Prop 或状态告诉复选框是否已选中。您还需要为每个复选框赋予一个值,这样当您选中它时,您实际上就有了一个可以使用的值。最后,您需要一个 onChange Prop ,该 Prop 将在单击复选框时发生。此函数应更新控制该特定复选框是否被选中的任何状态。这是为 React 制作的复选框输入示例

<input name={ this.props.name }
type="checkbox"
checked={ this.props.checked }
className="cui-checkbox-input"
onChange={ this._onChange }
value={ this.props.value } />

_onChange 是一个类似于

的函数
_onChange: function(event) { 
// do stuff using the event to grab needed values
}

关于javascript - 在 Reactjs 中选中/取消选中属性 onChange 复选框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39500059/

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