gpt4 book ai didi

javascript - If Else 条件在 Javascript 模板文字中不起作用

转载 作者:行者123 更新时间:2023-11-30 07:19:28 24 4
gpt4 key购买 nike

我在 tbody 中显示我的表数据时遇到问题,在我的 javascript 中我得到结果对象并且我在 javascript 文件中使用模板文字这是我的代码:

for (i = 0; i < size; i++) 
{
serialNo++;
html += `<tr>
<td>${serialNo}</td>
<td>${result[i].first_name}</td>
<td>${result[i].last_name}</td>
<td>${result[i].email}</td>
<td>${result[i].mobile}</td>
(${(result[i].status == 1)} ? '<td><div class="switchery-demo">
<input type="checkbox" checked id="demo${result[i].contact_id}"
onclick="status_item(this,'${result[i].contact_id}')"
data-plugin="switchery" data-size="small" data-color="#1bb99a"/>
</div></td>' : '<td><div class="switchery-demo"><input
type="checkbox" checked data-plugin="switchery"
id="demo${result[i].contact_id}"
onclick="status_item(this,'${result[i].contact_id}')" data-
size="small" data-color="#1bb99a"/></div></td>')
</tr>`;
}

在 result[i].status 中,我正在使用三元运算符,我正在检查 status == 1,然后先显示一个,然后再显示一个,但它正在创建两个而不是一个,我不知道我哪里出错了,请帮忙。

最佳答案

需要把整个部分移到表达式部分

`${ result[i].status == 1
? '...'
: 'some other'
}`

带有嵌套模板字符串的示例。

console.log(`${true === false
? `${ 10 }`
: `${ 20 }`
}`);

关于javascript - If Else 条件在 Javascript 模板文字中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57196924/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com