javascript - 在动态创建的 HTML 表中编写 Javascript If 条件

标签 javascript jquery html

我的JS

for (var i=0; i<result.length ;i++) {

                var selectValue = $("#ProductCode option[value=" + result[i].PRODUCT_CODE + "]").text();

                html += '<tr id="tr-'+tblcounter+'" class=""><td class="pc">'
                           + selectValue
                           + '</td><td class="oc">'
                           + result[i].ORDER_QTY
                            + '</td><td class="oc">'
                           + result[i].DELIVERY_QTY
                            + '</td><?php if($usertype == "ANE"){?><td class="oc">'
                           + result[i].RECIEVED_QTY
                            + '</td><?php } ?><td class="operations"><a href="#" class="editlines" id="'+result[i].LINE_NUMBER+'" onClick="editRow(this);"><img src="<?php echo $this->baseUrl(); ?>/images/edit_icon.png" height="20px" width="20px"></a>'

                           + '</td></tr>';
                   tblcounter++;
              }
              $('#recvLines').append(html);

在上表中,如果我的 Javascript 条件满足,我需要显示最后一列,否则我不想显示它。

例如

 + '</td><?php } ?>'+if("something" == "something")+'<td class="operations"><a href="#" class="editlines" id="'+result[i].LINE_NUMBER+'" onClick="editRow(this);"><img src="<?php echo $this->baseUrl(); ?>/images/edit_icon.png" height="20px" width="20px"></a>'

                           + '</td></tr>';

如果我像上面那样编写 if 条件,代码将无法正常工作。 有任何想法吗??谢谢

最佳答案

你可以使用三元运算符

+ '<tr>' + (x == 5 ? '<td>true</td>' : '<td>false</td>') + '</tr>'

关于javascript - 在动态创建的 HTML 表中编写 Javascript If 条件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21232237/

相关文章:

javascript - Google map 顶部的网格会产生正方形间隙

javascript - 这些代码的含义是什么? JavaScript

javascript - 为什么这个简单的 JQuery 代码会抛出错误?

javascript - 使用多级对象在 javascript 中发布请求

javascript - 如何通过名称调用函数并作为参数传递?

javascript - 获取计算具有类的元素的变量的长度,不起作用

javascript - 将 Google map 绘图库限制为一条折线/形状

javascript - 从 url 中删除特定的查询字符串参数值

html - 在div中填充图像

javascript - Chrome 应用程序创建的文件和文件夹位于文件系统的哪个位置?