尝试修复多个类别下拉列表

时间:2013-11-22 作者:clestcruz

我在尝试修复我正在开发的网站上的类别下拉列表时遇到了一些问题。基本上,一个下拉式过滤器有“按位置排序”,第二个是“按价格排序”。第一个过滤器工作正常,即“按位置排序”,但第二个过滤器工作不正常。它正在加载所有帖子,但没有正确过滤。

这里是链接-

http://digitalspin.ph/federalland/?page_id=23

这是我的密码

HTML-

<div class="filter_container">
        <?php wp_dropdown_categories( $args_cat1 ); ?> 
</div>

<div class="filter_container">
        <?php wp_dropdown_categories( $args_cat2 ); ?> 
</div>

JS-

<!--DROPDOWN SORT CATEGORY 1 -->
<script type="text/javascript">
var dropdown = document.getElementById("cat1");
function onCatChange() {
    if ( dropdown.options[dropdown.selectedIndex].value > 0 ) {
        location.href = "<?php echo get_option(\'home\');
?>/?cat="+dropdown.options[dropdown.selectedIndex].value;
    }
}
dropdown.onchange = onCatChange;
</script>


<!--DROPDOWN SORT CATEGORY 2-->
<script type="text/javascript">
var dropdown = document.getElementById("cat2");
function onCatChange() {
    if ( dropdown.options[dropdown.selectedIndex].value > 0 ) {
        location.href = "<?php echo get_option(\'home\');
?>/?cat="+dropdown.options[dropdown.selectedIndex].value;
    }
}
dropdown.onchange = onCatChange;
</script>

Functions.php

//LOCATION FILTER

$args_cat2 = array(
\'show_option_all\'    => \'\',
\'show_option_none\'   => \'\',
\'orderby\'            => \'ID\', 
\'order\'              => \'ASC\',
\'show_count\'         => 0,
\'hide_empty\'         => 1, 
\'child_of\'           => 0,
\'exclude\'            => \'1,2,3,4,32,33,34,35,36,37\',
\'echo\'               => 1,
\'selected\'           => 0,
\'hierarchical\'       => 0, 
\'name\'               => \'cat2\',
\'id\'                 => \'cat2\',
\'class\'              => \'postform\',
\'depth\'              => 0,
\'tab_index\'          => 0,
\'taxonomy\'           => \'category\',
\'hide_if_empty\'      => false,
\'walker\'             => \'\'
);

//PRICE FILTER

$args_cat1 = array(
\'show_option_all\'    => \'\',
\'show_option_none\'   => \'\',
\'orderby\'            => \'ID\', 
\'order\'              => \'ASC\',
\'show_count\'         => 0,
\'hide_empty\'         => 1, 
\'child_of\'           => 0,
\'exclude\'            => \'1,2,3,4,23,24,25,26,27,28,29,30,31\',
\'echo\'               => 1,
\'selected\'           => 0,
\'hierarchical\'       => 0, 
\'name\'               => \'cat1\',
\'id\'                 => \'cat1\',
\'class\'              => \'postform\',
\'depth\'              => 0,
\'tab_index\'          => 0,
\'taxonomy\'           => \'category\',
\'hide_if_empty\'      => false,
\'walker\'             => \'\'
);
以下是firebug的javascript错误-

TypeError: dropdown is null
dropdown.onchange = onCatChange;
每当我在“按价格排序”中选择一个类别时,它都会显示“按位置排序”中的所有帖子,即类别ID 23

----更新的代码----

<!--DROPDOWN SORT CATEGORY 1 -->
<script type="text/javascript">
var dropdown_price = document.getElementById("cat1");
function onCatChange() {
    if ( dropdown_price.options[dropdown_price.selectedIndex].value > 0 ) {
        location.href = "<?php echo get_option(\'home\');
?>/?cat="+dropdown_price.options[dropdown_price.selectedIndex].value;
    }
}
dropdown_price.onchange = onCatChange;
</script>


<!--DROPDOWN SORT CATEGORY 2-->
<script type="text/javascript">
var dropdown_location = document.getElementById("cat2");
function onCatChange() {
    if ( dropdown_location.options[dropdown_location.selectedIndex].value > 0 ) {
        location.href = "<?php echo get_option(\'home\');
?>/?cat="+dropdown_location.options[dropdown_location.selectedIndex].value;
    }
}
dropdown_location.onchange = onCatChange;
</script>

1 个回复
最合适的回答,由SO网友:Simon 整理而成

HTML似乎是正确的,因为价格下拉列表包含正确的cat ID。

我想这是一个JS问题,我的第一个猜测是,您不应该在两个脚本中使用相同的变量名。

将下拉列表替换为第二个下拉列表的dropdown\\u price,如:

<!--DROPDOWN SORT CATEGORY 2-->
<script type="text/javascript">
var dropdown_price = document.getElementById("cat2");
function onCatPriceChange() {
    if ( dropdown_price.options[dropdown_price.selectedIndex].value > 0 ) {
        location.href = "<?php echo get_option(\'home\'); ?>/?cat="+dropdown_price.options[dropdown_price.selectedIndex].value;
    }
}
dropdown_price.onchange = onCatPriceChange;
让我们知道!

结束