简体   繁体   English

jQuery.css('display')只返回内联

[英]jQuery.css('display') only returns inline

I am trying to get checked options from a table which are set inline . 我试图从inline设置的表中获取选中的选项。 There is a search function, which sets $(element).css('display','none') on objects in which there is no match with the search. 有一个搜索功能,它在与搜索不匹配的对象上设置$(element).css('display','none') Anyways, this piece of code will only return inline , no matter what the elements are set to. 无论如何,这段代码只会返回inline ,无论元素设置为什么。 Even if I manually set all of them to display: none in the table itself, the alert will return inline for every single object in the table. 即使我手动将所有这些设置为在表本身中display: none ,警报也将为表中的每个对象返回inline Is there any solution to this? 这有什么解决方案吗?

JS code: JS代码:

function pass_QR() {
    var i = 0;
    var array = [];
    $("input:checkbox:checked").each(function () {
        i++;
        alert($(this).css('display'));
        if ($(this).val() !== 0 && $(this).css('display') === 'inline') {
            array.push($(this).val());
        }
    });
}

Fundamentally, css("display") does work, so something else is going on. 从根本上说, css("display")确实有效,所以还有其他事情正在发生。

I suspect one of two things: 我怀疑两件事之一:

  1. The checkboxes that you're making display: none are never checked , and so you don't see them in your each loop. 您正在display: none的复选框display: none永远不会checked ,因此您不会在each循环中看到它们。

  2. You're not making the checkboxes display: none , but instead doing that to some ancestor element of them. 没有使复选框display: none ,而是对它们的某些祖先元素执行此操作。 In that case, $(this).is(":visible") is what you're looking for. 在这种情况下, $(this).is(":visible")就是你要找的东西。

Here's an example of #2: Live Copy | 以下是#2: Live Copy |的示例 Live Source 直播源

<div id="ancestor">
<input type="checkbox" checked>
</div>
<script>
$("#ancestor").css("display", "none");
console.log("display property is now: " +
    $("input:checkbox:checked").css("display"));
console.log("visible tells us what's going on: " +
    $("input:checkbox:checked").is(":visible"));
</script>

...which outputs: ......哪个输出:

display property is now: inline-block
visible tells us what's going on: false

Applying that to your code: 将其应用于您的代码:

function pass_QR() {
    var i = 0;
    var array = [];
    $("input:checkbox:checked").each(function () {
        i++;
        alert($(this).css('display'));
        if ($(this).val() !== 0 && $(this).is(':visible')) {
        // Change is here -----------------^^^^^^^^^^^^^^
            array.push($(this).val());
        }
    });
}

Side note: Every time you call $() , jQuery has to do some work. 旁注:每次调用$() ,jQuery都要做一些工作。 When you find yourself calling it repeatedly in the same scope, probably best to do that work once : 当你发现自己在同一范围内反复调用它时,最好做一次这样的工作:

function pass_QR() {
    var i = 0;
    var array = [];
    $("input:checkbox:checked").each(function () {
        var $this = $(this); // <=== Once
        i++;
        alert($this.css('display'));
        if ($this.val() !== 0 && $this.is(':visible')) {
        // Other change is here -------^^^^^^^^^^^^^^
            array.push($this.val());
        }
    });
}

try following: 尝试以下:

$("input:checkbox:checked").each(function(i,o){       
      console.log($(this).css("display"));       
});

working fiddle here: http://jsfiddle.net/BcfvR/2/ 在这里工作小提琴: http//jsfiddle.net/BcfvR/2/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM