我有候选人名单作为父母和候选人作为孩子。在候选列表上有一个 Select All 输入,因此我将函数绑定到它以设置状态,如果正在选择候选并将该状态传递给孩子。这一部分有效,但这些输入本身不可变,仅通过父 Select All 可以更改。
CandidateList 组件(父):
class CandidateList extends React.Component {
constructor(props) {
super(props);
this.state = {
candidateList: null,
candidateSelected: false
}
}
onSelectCandidatesClick() {
this.setState({
candidateSelected: !this.state.candidateSelected
});
}
render() {
return (
<div className="CandidateList">
<div className="__selection">
<input className="checkbox" type="checkbox" id="selectAll" onClick={() => this.onSelectCandidatesClick()}/>
<label htmlFor="selectAll"><span id="__label">Select All</span></label>
</div>
</div>
);
}
}
Run Code Online (Sandbox Code Playgroud)
候选组件(子):
class Candidate extends React.Component {
constructor(props) {
super(props);
this.state = {
candidateSelected: false
}
}
componentWillReceiveProps(nextProps) {
this.setState({
candidateSelected: nextProps.selectCandidate
}); …Run Code Online (Sandbox Code Playgroud) 我现在尝试:
this值DONE4.并将最大值parent背景设置为例如background: #000;
所以我的问题是我将如何获得之前计算出的MAX值的父级?
$('.views').each(function(){
valText = $(this).text();
$(this).attr('value', valText);
});
var numbers = $(".views").map(function(){
return parseFloat(this.getAttribute('value')) || -Infinity;
});
var calculate = Math.max.apply(Math, numbers);
$("#max").html(calculate);
Run Code Online (Sandbox Code Playgroud)
<div><div class="views" >10</div></div>
<div><div class="views" >1</div></div>
<div><div class="views" >7</div></div>
<div><div class="views" >5</div></div>
<div><div class="views" >3</div></div>
Run Code Online (Sandbox Code Playgroud)
最大值是:
<p>Max value is: <span id="max"></span></p>
Run Code Online (Sandbox Code Playgroud)