javascript - 如果选择特定单选按钮,则显示输入框
问题描述
目标:
如果您在收音机中选择绿色。输入框文本将出现在单选输入组下方。如果单击另一个单选按钮,它将不会显示。
如果您在收音机中选择蓝色。输入框文本将出现在单选输入组下方。如果单击另一个单选按钮,它将不会显示。
问题:
我不知道如何解决它。你会怎么解决?
信息:
* reactjs 中的新手
Stackblitz:
https ://stackblitz.com/edit/react-rpncls ?
谢谢!
import React from 'react';
import './style.css';
export default class App extends React.Component {
constructor(props) {
super(props);
this.state = {
color: 'red'
};
this.onRadioChange = this.onRadioChange.bind(this);
}
onRadioChange = e => {
this.setState({
color: e.target.value
});
};
render() {
return (
<div className="App">
<form onSubmit={this.onSubmit}>
<strong>Select Color:</strong>
<ul>
<li>
<label>
<input
type="radio"
value="red"
checked={this.state.color === 'red'}
onChange={this.onRadioChange}
/>
<span>Red</span>
</label>
</li>
<li>
<label>
<input
type="radio"
value="green"
checked={this.state.color === 'green'}
onChange={this.onRadioChange}
/>
<span>Green</span>
</label>
</li>
<li>
<label>
<input
type="radio"
value="blue"
checked={this.state.color === 'blue'}
onChange={this.onRadioChange}
/>
<span>Blue</span>
</label>
</li>
<li>
<label>
<input
type="radio"
value="orange"
checked={this.state.color === 'orange'}
onChange={this.onRadioChange}
/>
<span>Ornage</span>
</label>
</li>
<li>
<label>
<input
type="radio"
value="purple"
checked={this.state.color === 'purple'}
onChange={this.onRadioChange}
/>
<span>Purple</span>
</label>
</li>
</ul>
<button type="submit">Choose Color</button>
</form>
</div>
);
}
}
解决方案
您可以根据所选颜色有条件地渲染文本框。
应用程序.js:
import React from 'react';
import './style.css';
export default class App extends React.Component {
constructor(props) {
super(props);
this.state = {
color: 'red'
};
this.onRadioChange = this.onRadioChange.bind(this);
}
onRadioChange = e => {
this.setState({
color: e.target.value
});
};
textBoxVisible = () => {
return (this.state.color==="green" || this.state.color==="blue");
}
render() {
return (
<div className="App">
<form onSubmit={this.onSubmit}>
<strong>Select Color:</strong>
<ul>
<li>
<label>
<input
type="radio"
value="red"
checked={this.state.color === 'red'}
onChange={this.onRadioChange}
/>
<span>Red</span>
</label>
</li>
<li>
<label>
<input
type="radio"
value="green"
checked={this.state.color === 'green'}
onChange={this.onRadioChange}
/>
<span>Green</span>
</label>
</li>
<li>
<label>
<input
type="radio"
value="blue"
checked={this.state.color === 'blue'}
onChange={this.onRadioChange}
/>
<span>Blue</span>
</label>
</li>
<li>
<label>
<input
type="radio"
value="orange"
checked={this.state.color === 'orange'}
onChange={this.onRadioChange}
/>
<span>Ornage</span>
</label>
</li>
<li>
<label>
<input
type="radio"
value="purple"
checked={this.state.color === 'purple'}
onChange={this.onRadioChange}
/>
<span>Purple</span>
</label>
</li>
</ul>
{this.textBoxVisible() && <input type="text"/>}
<button type="submit" class="submitButton">Choose Color</button>
</form>
</div>
);
}
}
要确保文本框和提交按钮不在同一行,请将其添加到 style.css:
.submitButton{
display:block;
}
推荐阅读
- python-3.x - Python:使用一组邻接列表构建图
- android - 当用户单击 appBar 中的“+”号时,如何向 ListView 添加卡片?
- python - Python对象数组的奇怪行为
- python - 在计算中使用 Randint 数
- audio - 当您浏览静态网站时,有没有办法让背景视频继续播放?
- encryption - 通过 SSL 发送整数而不是字符串以节省带宽是个好主意吗?
- ffmpeg - 无法为 ' 找到合适的输出格式:参数无效
- java - Java 练习:我不确定自己做错了什么,感谢所有帮助
- python - 当 Python 并发未来对象完成时,如何停止主线程而不进行轮询?
- ssis - 如何构建 ODS 层?