javascript - 在列的特定值上连续隐藏按钮

标签 javascript jquery

我有一个自动生成的表格,其中包含任务及其状态。每行都有一个按钮来重新启动任务。

如果任务的值不等于“错误”,我想隐藏重启按钮。所以换句话说:只有带有 Status==Error 的任务应该有一个可见的重启按钮

这是一个 fiddle 链接:https://jsfiddle.net/hwqt7c3a/5/

我试过:

window.onload = function () {
            $(function () {
                $('table tr').each(function () {
                    var Cells = this.getElementsByTagName("td");
                    if (Cells[2].innerText !== 'Error') {
                        $(this).find('button').style.visibility = 'hidden';
                    }
                });
            });
        }

但由于某些原因,Cells 始终为空。

最佳答案

我已经更新了你的 fiddle ,这是 working fiddle

$(function() {                             //document ready event
  $('table tr').each(function() {          //loop all tr's
    var Cell = $(this).find('td:eq(2)');   //find the 3rd td cell
    if (Cell.text() !== 'Error') {         //check if its text is "Error" 
      $(this).find('button').hide();       //if try then find the button of this tr and hide it          
    }
  });
});

不要混合使用 javascript 和 Jquery 语法,

你也不需要这两行

window.onload = function () { // javascript way of handling document ready
            $(function () {   // jquery way of handling document ready.

使用任何一个,因为你正在使用 Jquery 库,那么你最好使用它

$(function () {

这样你就不会混淆语法..


如果您希望 td 占用 UI 中的按钮空间但仍被隐藏,那么您可以使用

$(this).find('button').css("visibility","none")

关于javascript - 在列的特定值上连续隐藏按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36836587/

相关文章:

javascript - 将实时数据传输到网页

javascript - IE7 中的 jQuery 属性

javascript - 如何计算输入的字符数及其操作?

javascript - 主干嵌套 View 无法找到 id 选择器

javascript - 使用数组检查字符串长度

javascript - JavaScript中instanceof是如何实现的

javascript - angular js中没有触发更改事件

geoxml3 afterParse回调的javascript范围错误

jquery - 如何从这个 html date-ranger 中获取值(value)?

javascript - Symfony2 Ajax 和 Jquery