如何仅禁用使用复选框的输入字段?
我想使用复选框禁用“完成日期”字段。
问题是我动态创建了这些字段,因此,当我尝试仅禁用与之相关的字段时,它会禁用所有其他“完成日期”字段,我希望仅禁用上一个字段,而不是所有其他字段。
const [checked, setChecked] = useState(false)
const [inputList, setInputList] = useState([
{
startDate: "Start Date",
completionDate: "Completion Date:",
inline: true
}
])
const handleInputChange = (e, index, val) => {
const { name, value } = e.target
setChecked(e.target.checked)
const list = [...inputList]
list[index][name] = val !== undefined ? val : value
setInputList(list)
}
const handleRemoveClick = (index) => {
const list = [...inputList]
list.splice(index, 1)
setInputList(list)
}
const handleAddClick = () => {
setInputList([
...inputList,
{
startDate: "Start Date",
completionDate: "Completion Date:",
inline: true
}
])
}
let embed = {
embed: {
fields: inputList
}
}
//Form
{inputList.map((x, i) => {
return (
<>
<FormControl>
<Box>
<FormLabel htmlFor='startDate'>Start Date:</FormLabel>
<Input
name="startDate"
onChange={(e) => handleInputChange(e, i)}
type='text'
placeholder='Start Date'
/>
</Box>
<Box>
<FormLabel htmlFor='completionDate'>Completion Date:</FormLabel>
<Input
name="completionDate"
isDisabled={checked ? true : false}
onChange={(e) => handleInputChange(e, i)}
type='text'
placeholder='Completion Date'
/>
</Box>
<Box pt={{ base: '0', md: '2.7rem'}}>
<Checkbox
type="checkbox"
name="inline"
checked={x.inline}
onChange={(e) => handleInputChange(e, i, e.target.checked)}
colorScheme='purple'>
<label>Studying</label>
</Checkbox>
</Box>
</FormControl>
{inputList.length - 1 === i && (
<Button onClick={handleAddClick}> + New Section </Button>
)}
{inputList.length !== 1 && (
<Button onClick={() => handleRemoveClick(i)}>- Remove Section</Button>
)}
</>
)
})}
I would like to disable the "Date of Completion" field with the checkbox.
The problem is that I created these fields dynamically, so when I try to disable only the field relating to it, it disables all other "Date of Completion" fields I would like to disable only the previous field and not all others.
const [checked, setChecked] = useState(false)
const [inputList, setInputList] = useState([
{
startDate: "Start Date",
completionDate: "Completion Date:",
inline: true
}
])
const handleInputChange = (e, index, val) => {
const { name, value } = e.target
setChecked(e.target.checked)
const list = [...inputList]
list[index][name] = val !== undefined ? val : value
setInputList(list)
}
const handleRemoveClick = (index) => {
const list = [...inputList]
list.splice(index, 1)
setInputList(list)
}
const handleAddClick = () => {
setInputList([
...inputList,
{
startDate: "Start Date",
completionDate: "Completion Date:",
inline: true
}
])
}
let embed = {
embed: {
fields: inputList
}
}
//Form
{inputList.map((x, i) => {
return (
<>
<FormControl>
<Box>
<FormLabel htmlFor='startDate'>Start Date:</FormLabel>
<Input
name="startDate"
onChange={(e) => handleInputChange(e, i)}
type='text'
placeholder='Start Date'
/>
</Box>
<Box>
<FormLabel htmlFor='completionDate'>Completion Date:</FormLabel>
<Input
name="completionDate"
isDisabled={checked ? true : false}
onChange={(e) => handleInputChange(e, i)}
type='text'
placeholder='Completion Date'
/>
</Box>
<Box pt={{ base: '0', md: '2.7rem'}}>
<Checkbox
type="checkbox"
name="inline"
checked={x.inline}
onChange={(e) => handleInputChange(e, i, e.target.checked)}
colorScheme='purple'>
<label>Studying</label>
</Checkbox>
</Box>
</FormControl>
{inputList.length - 1 === i && (
<Button onClick={handleAddClick}> + New Section </Button>
)}
{inputList.length !== 1 && (
<Button onClick={() => handleRemoveClick(i)}>- Remove Section</Button>
)}
</>
)
})}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您应该将
检查
添加到inputList
对象。You should add
checked
toinputList
object.您的完成日期输入
ISDISABLED
属性取决于一个检查
状态,该状态与所有完成日期输入共享。因此,当您单击任何复选框时,检查状态将通过
handle InputChange
更新,并且所有完成日期输入将处于禁用/启用模式,具体取决于最新的单击复选框检查值。如 @hossein ,您需要添加
检查
属性。所以,让我们去做吧。首先,更新您的
inputList
初始化:下一步,更新
handleinputChange
方法:更新您的handleaddclick方法:
下一步,在复选框中 和最后,更新您的
onChange
组件:进行这些更新后,您的代码应起作用。
这是有效的最小示例:
Your completion date input
isDisabled
property depends on onechecked
state which is shared with all completion date input.So, when you click any checkbox, the checked state will be updated by
handleInputChange
and all completion date input will be in disabled/enabled mode, depending on the latest clicked checkbox checked value.As said by @hossein, you need to add
checked
property. So, let's do it.First, update your
inputList
initialization:Next, update your
handleInputChange
method:Next, update your handleAddClick method:
And finally, update your
onChange
at Checkbox component:Your code should work after you made those updates.
Here is the minimal example that works: