英文:
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 ?:
<td>${value['status'] == 'false' ? value['identifier'] : value['status']}</td>
答案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['status'] ? value['status'] : value['identifier']
Then you can use it as <td>{status}</td>
答案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 === 'something' ? 'something' : 'not something'
construction may be used directly inside ${ }
block:
<!-- 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 -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论