【问题标题】:JQuery Won't Filter Using a ValueJQuery 不会使用值进行过滤
【发布时间】:2013-05-20 07:07:50
【问题描述】:

我正在尝试创建一个简单的函数,当一个 div 中的所有下拉框都被选中时,它会告诉我。问题似乎是过滤器没有选择任何东西,所以长度总是为 0。知道会发生什么吗?到目前为止,这是我所拥有的:

jQuery

$('.month, .year').change(function () {
    var $this = $(this);
    var $empty = $this.parent().parent().find('.month, .year').filter('[value=""]').length;
    if ($empty == 0) {
        alert('there are no empty dropdowns');
    }
});

HTML

<form class="date_catcher">
    <div style="display:inline;" class="start">
        <select class='input-small month' name='month'>
            <option value='' selected='selected'>---</option>
            <option value='0'>Jan</option>
        </select>
        <select class='input-small year' name='year'>
            <option value='' selected='selected'>----</option>
            <option value="2012">2012</option>
        </select>
    </div>

    <div style="display:inline;" class="end">
        <select class='input-small month' name='month'>
            <option value='' selected='selected'>---</option>
            <option value='0'>Jan</option>
        </select>
        <select class='input-small year' name='year'>
            <option value='' selected='selected'>----</option>
            <option value="2012">2012</option>
        </select>
    </div>
</form>

感谢您的帮助!

【问题讨论】:

  • .month.year 是选择元素而不是选项。注意select 元素没有value 属性。

标签: jquery drop-down-menu filter


【解决方案1】:

试试这个 -

var $empty = $this.closest('form').find('.month, .year').filter(function(){
  return this.value === "";
}).length;

演示--&gt;http://jsfiddle.net/J8x5X/

【讨论】:

【解决方案2】:
$(".date_catcher select").change(function(){
    $empty = $(".date_catcher option").filter(":selected").filter("[value='']").size();
    if($empty == 0){
        alert("there are no empty dropdowns");
    }
});

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-08-07
    • 1970-01-01
    • 2021-11-07
    • 2016-09-26
    • 2015-01-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多