2016-08-02 37 views
0

我有以下的jQuery根据data-filter属性显示/隐藏的div:数据选择引导按钮属性在页面加载

$(document).ready(function(){ 
$(".filter-button").click(function(){ 
    var value = $(this).attr('data-filter'); 
    if(value == "all") 
    { 
     $('.filter').show('1000'); 
    } 
    else 
    { 
     $(".filter").not('.'+value).hide('3000'); 
     $('.filter').filter('.'+value).show('3000'); 
    } 
}); 

我也有这个jQuery进行排序的div:

​​

下面是我用来过滤的DIV的HTML:

<div class="btn-group" data-toggle="buttons"> 
    <label class="btn btn-default filter-button" data-active-class="location-filter" data-filter="all"><input type="radio" name="options" id="all" autocomplete="off" checked>All</label> 
    <label class="btn btn-default filter-button" data-active-class="location-filter" data-filter="new"><input type="radio" name="options" id="new" autocomplete="off" checked>Newcastle</label> 
    <label class="btn btn-default filter-button" data-active-class="location-filter" data-filter="man"><input type="radio" name="options" id="man" autocomplete="off" checked>Manchester</label> 
    <label class="btn btn-default filter-button" data-active-class="location-filter" data-filter="dub"><input type="radio" name="options" id="dub" autocomplete="off" checked>Dublin</label> 
    <label class="btn btn-default filter-button" data-active-class="location-filter" data-filter="win"><input type="radio" name="options" id="win" autocomplete="off" checked>Winnersh</label> 
    <label class="btn btn-default filter-button" data-active-class="location-filter" data-filter="lon"><input type="radio" name="options" id="lon" autocomplete="off" checked>London</label> 
</div> 
<br /> 
<div class="btn-group" data-toggle="buttons"> 
    <label id="alpha-button-az" class="btn btn-default" data-active-class="alpha-sort"><input type="radio" name="options" autocomplete="off" checked>A-Z</label> 
    <label id="alpha-button-za" class="btn btn-default" data-active-class="alpha-sort"><input type="radio" name="options" autocomplete="off" checked>Z-A</label> 
</div> 

我想要做的是什么能够在页面加载时选择每个按钮中的一个。

到目前为止,我有这样的:

$(document).ready(function() { 
    $("#alpha-button-az").click(); 
    $(".filter-button").click(); 
}); 

这适用于(在页面加载选择与AZ)的AZ按钮,但由于过滤器使用自定义属性,我不知道如何选择一个特定的按钮。目前,它正在选择组中的最后一个按钮。

任何帮助,非常感谢。

+0

你所说的“选择每个按钮的一个”意思?在运行它的功能? –

+0

我希望其中一个按钮被“点击”,以便它们显示为活动状态。例如:https://snag.gy/y0O7mx.jpg 这是目前发生的事情:https://snag.gy/8puhsw.jpg – Kungfauxn00b

+0

请参阅我提供的答案。希望这有效!我不确定你在Bootstrap中使用了什么元素,看起来像这样,我找不到它。如果您向我发送文档参考,那么我可以提供更多帮助。 –

回答

0

这里的第一个代码片段,略有变化:

$(document).ready(function(){ 
$(".filter-button").click(function(){ 
    var value = this.dataset.filter; // Note this code change btw! 

    if(value == "all") 
    { 
     $('.filter').show('1000'); 
    } 
    else 
    { 
     $(".filter").not('.'+value).hide('3000'); 
     $('.filter').filter('.'+value).show('3000'); 
    } 
}); 

与第二:

function sortAlphaAscending() { 
    var alphabeticallyOrderedDivs = $divs.sort(function(a,b){ 
     return $(a).find("h2").text() > $(b).find("h2").text(); 
    }); 
    $(".profile-container").html(alphabeticallyOrderedDivs); 
} 

function sortAlphaDescending() { 
    var alphabeticallyOrderedDivs = $divs.sort(function(b,a){ 
     return $(a).find("h2").text() > $(b).find("h2").text(); 
    }); 
    $(".profile-container").html(alphabeticallyOrderedDivs); 
} 

$('#alpha-button-az').click(sortAlphaAscending); 
$('#alpha-button-za').click(sortAlphaDescending); 

现在,而不是 “调用” 事件处理程序,只需调用你想要的功能:

$(document).ready(function() { 
    // Do functions here to data 
    sortAlphaAscending(); 

    // Activate elements here 
    $(".selector").attr("active"); 
}); 

免责声明:我99%确定要添加active属性,但我没有测试过。

编辑:此更新的HTML可以工作。请将active属性添加到要开始作为活动元素的每个元素。我看不到你在你的问题的按钮HTML,所以作为一个例子:

<button id="some-button">...</button> 

将成为

<button id="some-button" active>...</button> 
+0

感谢队友,所有按钮都可以正常工作,但我不认为我用自己想要的方式很好地解释了自己...... :) 目前,当页面加载时,所有div都可见且未排序。然后他们可以选择“位置”按钮来过滤div。他们还可以点击按钮按字母顺序排序。 我想要的是为其中一个“位置”按钮进行选择,并自动过滤div。我也希望在页面加载时发生这种情况。 – Kungfauxn00b

+0

@ Kungfauxn00b不用担心!你需要什么? –

+0

使用代码的最后一位调用该函数会对div进行排序,但“A-Z”按钮不显示为活动状态。 另外,我希望“所有”位置按钮显示为活动状态。 – Kungfauxn00b