编辑到完整场景:我有两个组件,四个按钮!三个按钮的默认值是null
。让我们说a,b,c,d是各种四个按钮。
null
,因此,按钮d将被禁用。null
,当它们都获得值onPressing
时,将再次启用按钮d。请注意,组件是有条件地呈现的,因此用户眼睛只能看到一个组件!
这是我的代码:
<Button
disabled={(this.state.quantityItemSelected === null ||
this.state.deliveryOptionSelected === null) &&
(this.state.timeSlotItemSelected === null) ? true : false}
/>
默认常量是this.state.quantityItemSelected === null
,两者都相同。
它适用于()
的条件,但关于第一,当this.state.quantityItemSelected
最初不是null
然后this.state.timeSlotItemSelected
变得不是null
它工作正常,
但当我反过来说,当this.state.timeSlotItemSelected
最初变为空时,条件变为假。
我的预期工作是,当任何事情是null
与常数时应该是真的。
请帮助解决这个问题,让我知道还需要了解的其他内容
如果我有a,b,c当a = null且b = null则它应该为真,当a = null且c = null时它应该为真,休息条件为假。
尝试以下,它会工作!你可以用它。
(this.state.quantityItemSelected === null || this.state.deliveryOptionSelected ===
null) && (this.state.timeSlotItemSelected === null || this.state.quantityItemSelected
=== null) ? true : false
如果它正常工作,请告诉我!
例如 - 如果我有a,b,c当a = null且b = null则它应该为true,当a = null且c = null时它应该为true,rest条件为false。
您需要将后两个条件括起来并使用||
。
<Button
disabled={(
(this.state.quantityItemSelected === null) &&
(this.state.deliveryOptionSelected === null || this.state.timeSlotItemSelected === null)
) ? true : false}
/>
例如 - 如果我有a,b,c当a = null且b = null则它应该为true,当a = null且c = null时它应该为true,rest条件为false。
你(伪逻辑符号)是扩展的(!a && !b && c) || (!a && b && !c)
意味着什么
function calculateConditions(a, b, c) {
return (a === null && b === null && c !== null) || (a === null && b !== null && c === null);
}
//T - true, F - False. Shortenned to keep the formatting
const table = [
["FFF", calculateConditions( null , null , null)],
["FFT", calculateConditions( null , null , "not null")],
["FTF", calculateConditions( null , "not null", null)],
["FTT", calculateConditions( null , "not null", "not null")],
["TFF", calculateConditions("not null", null , null)],
["TFT", calculateConditions("not null", null , "not null")],
["TTF", calculateConditions("not null", "not null", null)],
["TTT", calculateConditions("not null", "not null", "not null")],
]
for (const [key, value] of table) {
console.log(key, value)
}
假设'this.state.quantityItemSelected'是'a','this.state.deliveryOptionSelected'是'b'而'this.state.timeSlotItemSelected'是'c',条件应该是:
this.state.quantityItemSelected === null &&
(this.state.deliveryOptionSelected === null ||
this.state.timeSlotItemSelected === null) ? true : false
或者a,b和c
a === null && (b === null || c === null) ? true : false
如果零,空字符串等假值不是a,b和c的有效值,则可以通过执行以下操作来改进代码:
disable = !a && (!b || !c)
回答编辑过的问题
在第一个组件中,按钮a和b默认为null,因此按钮d将被禁用。当用户按下a&b时,将为a&b分配一个值,因此在分配&b后,应启用d。
这与前一个不同。
disableButtonD = a === null && b === null;
或更好
disableButtonD = !(a && b);
并为:
在第二个组件中,按钮a和c的默认值为null,当它们都有值onpress时,再次启用按钮d。
disableButtonD = a === null && C === null;
或更好
disableButtonD = !(a && C);
遵循约束条件,您不能对两种情况使用一个句子。当然,根据您使用的框架,您可以拥有一个功能。
角度示例:
零件:
disableD(firstValue, secondValue) {
return !(firstValue && secondValue)
}
HTML
<!-- first Button -->
<button [disabled]="disableD(a, b)"></button>
<!-- second button-->
<button [disabled]="disableD(a, c)"></button>