简体   繁体   English

三元运算符检查多个字符串

[英]Ternary Operator to check more than one string

I have a piece of code where I am checking if the value is null or turnedoff then put a blank space in the variable at lefthand side. 我有一段代码正在检查值是否为null或已turnedoff然后在左侧的变量中放置blank space But for some reason the turnedoff is not getting replaced. 但是由于某种原因, turnedoff状态并未得到取代。 What am I doing wrong? 我究竟做错了什么?

JS : JS:

$scope.ModelAux ={
  ssclientID: sessionStorage.clientId === ('null' || 'turnedoff') ? '' : sessionStorage.clientId
};

alert($scope.ModelAux.ssclientID);

CSHTML : CSHTML:

<input type="text" name="clientID" class="form-control input-md" ng-model="ModelAux.ssclientID" required/>

This 这个

sessionStorage.clientId === ('null' || 'turnedoff') ?

should be 应该

sessionStorage.clientId === 'null' || sessionStorage.clientId ===  'turnedoff' ?

because 因为

'null' || 'turnedoff'

is always 'null' and does not check if the value is 'turnedoff' . 始终为'null' ,并且不检查值是否为'turnedoff'

what @Nina Scholz says is correct. @Nina Scholz说的是正确的。 If you want to get more terse you could also do: 如果您想变得更简洁,也可以执行以下操作:

['null','turnedoff'].indexOf(sessionStorage.clientId) >= 0 ? '', sessionStorage.clientId;

尝试跟随

(!sessionStorage.clientId || sessionStorage.clientId ===  'turnedoff') ? ''

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM