【发布时间】:2021-02-11 08:14:51
【问题描述】:
我这里有这个按钮
<button className={Classes.Button}
disabled={!isEnabled}
type="submit">
{buttonText}
</button>
在检查我的一些输入值后应该禁用或启用它。 条件
const canBeSubmitted = () => {
return (
customerData.firstNameState.length > 0 && // TextInput
customerData.emailState.length > 0 && // TextInput
customerData.companeyState.length > 0 && // TextInput
customerData.selected.length > 0 && // Dropdown
customerData.agree === true // checkbox for terms
);
};
let isEnabled = canBeSubmitted();
顺便说一句:同意复选框由其处理程序选中并且工作正常。 状态和处理程序中的agreement值为false
const handleChange = (event) => {
const field = event.target.id;
if (field === "firstName") {
setFirstName({ firstName: event.target.value });
} else if (field === "email") {
setEmail({ email: event.target.value });
} else if (field === "country") {
setSelected({ country: event.target.value });
} else if (field === "agree") {
setAgree(!agree);
console.log(agree);
}
};
但总是返回 false。我错过了什么?
请帮帮我
【问题讨论】:
-
你在复选框中传递了什么值?
===是一个严格的相等比较运算符,如果您将 true 作为字符串传递,那么它可能会导致问题。 -
你检查了很多东西,也许你应该把它们全部注释掉,然后只注释一个,看看哪个语句导致了问题。还有为什么你有 !isEnabled 而不仅仅是 isEnabled?
-
@devin 我按照你的建议做了谢谢,问题来自前三个条件。但是如何检查用户是否输入了输入?
-
如果
costumerData.selected是一个复选框,则不应检查其长度是否有效。 -
@steven7mwesigwa 谢谢,没有costumerData.selected 是一个下拉菜单,agree 是一个用于条款的复选框,另外3 个是文本输入。
标签: javascript html reactjs jsx