It is possible to add the if condition inside `${‘code here’}`.

huangapple go评论49阅读模式
英文:

Is it possible to add the if condition inside `${ 'code here' }`

问题

$('#inactive_list_body').append(<tr> <td>${value['name']}</td> <td>${ if(value['status'] == 'false'){ value['identifier']}else{value['status']} }</td> // ^^^^ 这里我需要添加IF条件 <td>${value['reason_without_or_inactive']}</td> </tr>)

英文:

How can I insert the if condition inside of ${ } using jQuery.

$('#inactive_list_body').append(`
<tr>
	<td>${value['name']}</td> 
	<td>${ if(value['status'] == 'false'){           
	   value['identifier']}else{value['status']} }</td> 
	// ^^^^ Here I need to add IF condition
	<td>${value['reason_without_or_inactive']}</td>
</tr>
`);

答案1

得分: 1

可以使用三元运算符 ?:

<td>${value['status'] == 'false' ? value['identifier'] : value['status']}</td>
英文:

You can use the ternary operator ?:

 &lt;td&gt;${value[&#39;status&#39;] == &#39;false&#39; ? value[&#39;identifier&#39;] : value[&#39;status&#39;]}&lt;/td&gt;

答案2

得分: 0

你可以在你的附加方法之外设置那个变量。

let status = value['status'] ? value['status'] : value['identifier']

然后你可以这样使用它:<td>{status}</td>

英文:

You could set that variable outside of your append method.

let status = value[&#39;status&#39;] ? value[&#39;status&#39;] : value[&#39;identifier&#39;]

Then you can use it as &lt;td&gt;{status}&lt;/td&gt;

答案3

得分: 0

var === 'something' ? 'something' : 'not something' 结构可以直接在 ${ } 块内使用:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-html -->
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <body>
       <p></p>
       <script>
           var a = 'a';
           $('p').append(
             `<tr><td>${a === 'a' ? 'a is a' : 'a is not a'}</td></tr>`
           );
       </script>
    </body>

<!-- end snippet -->
英文:

var === &#39;something&#39; ? &#39;something&#39; : &#39;not something&#39; construction may be used directly inside ${ } block:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-html -->

&lt;script src=&quot;https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js&quot;&gt;&lt;/script&gt;
&lt;body&gt;
   &lt;p&gt;&lt;/p&gt;
   &lt;script&gt;
       var a = &#39;a&#39;;
       $(&#39;p&#39;).append(
         `&lt;tr&gt;&lt;td&gt;${a === &#39;a&#39; ? &#39;a is a&#39; : &#39;a is not a&#39;}&lt;/td&gt;&lt;/tr&gt;`
       );
   &lt;/script&gt;
&lt;/body&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年4月4日 09:27:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/75924831.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定