【问题标题】:code not working without an alert() in javascriptjavascript 中没有 alert() 的代码无法工作
【发布时间】:2014-11-12 14:03:16
【问题描述】:

我的问题是以下代码在没有 alert() 的情况下无法工作。我正在使用两级选择/取消选择所有框。但代码仅适用于一个级别。在没有警报的情况下,取消选中单个复选框时无法取消选中“全选”复选框,反之亦然..

alert('17');

$('input.DataCheckAll').click(function() {
    if ($('input.DataCheckAll').length == $('input.DataCheckAll:checked').length) {
        $('input.CheckAll').prop('checked', true);
    } else {
        $('input.CheckAll').prop('checked', false);
    }
});

if ($('input.CheckAll').length > 0) {
    $('input.CheckAll').attr('checked', false);
    $('input.CheckAll').click(function() {
        if (this.checked) {
            $('input.DataCheckAll').each(function() {
                this.checked = true;
            });
        } else {
            $('input.DataCheckAll').each(function() {
                this.checked = false;
            });
        }
    });
}

【问题讨论】:

  • hmmm...奇怪,你的帖子也能对应HTML吗?
  • 您是否已经尝试过其他方法而不是警报?就像console.log('17); 一样,我以为我曾经遇到过类似的问题,但再次查看代码我发现问题实际上与alert(); 无关,而是与页面(重新)加载上的输入值有关。我在选择器上犯了一个错误。
  • console.log() 也无济于事
  • 我可以使用 setTimeout() 吗?如果可以,如何?

标签: javascript jquery


【解决方案1】:

你应该在 DOM 准备好之后执行你的 jquery 脚本,所以把它包裹在$(function(){});

注意 - 此外,您无需使用 .each() 迭代 $('input.DataCheckAll') 来选中/取消选中。你可以简单地使用$('input.DataCheckAll').prop('checked',true);

$(function(){
$('input.DataCheckAll').click(function() {
    if ($('input.DataCheckAll').length == $('input.DataCheckAll:checked').length) {
        $('input.CheckAll').prop('checked', true);
    } else {
        $('input.CheckAll').prop('checked', false);
    }
});

if ($('input.CheckAll').length > 0) {
    $('input.CheckAll').attr('checked', false);
    $('input.CheckAll').click(function() {
        /*if (this.checked) {
            $('input.DataCheckAll').each(function() {
                this.checked = true;
            });
        } else {
            $('input.DataCheckAll').each(function() {
                this.checked = false;
            });
        }*/
      // to select / deselect all data check boxes
      $('input.DataCheckAll').prop('checked',this.checked);
    });
}
});

【讨论】:

    【解决方案2】:

    很有可能您只需将其包装在$(function() { /* code */ }); 中。目前,您的代码正在被警报停止,这让文档在后台加载,因此当您关闭警报时,页面已准备好执行您尝试执行的所有操作。

    只需告诉它等到页面完成加载,您就不再需要警报了。

    $(function() {
        // code
    });
    

    完全一样
    $(document).ready(function() {
        // code
    });
    

    【讨论】:

    • 我尝试将它包装在 $(function(){});但它不工作。同样的结果
    • 好的,然后链接到您的页面 - 否则我们只是猜测和猜测和猜测
    【解决方案3】:

    代码可能在 dom 准备好之前就已经运行了,试试这个:

    $(function(){     //by passing jQuery a function instead of a selector
                      // it will call the function when the dom is ready
    $('input.DataCheckAll').click(function() {
        if ($('input.DataCheckAll').length == $('input.DataCheckAll:checked').length) {
            $('input.CheckAll').prop('checked', true);
        } else {
            $('input.CheckAll').prop('checked', false);
        }
    });
    
    if ($('input.CheckAll').length > 0) {
        $('input.CheckAll').attr('checked', false);
        $('input.CheckAll').click(function() {
            if (this.checked) {
                $('input.DataCheckAll').each(function() {
                    this.checked = true;
                });
            } else {
                $('input.DataCheckAll').each(function() {
                    this.checked = false;
                });
            }
        });
    }
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-11-10
      • 1970-01-01
      • 1970-01-01
      • 2012-09-10
      • 2019-01-28
      • 2014-12-06
      • 2019-11-08
      相关资源
      最近更新 更多