0
我在我的WP主题中使用了下拉菜单,并使用了选择框。在一些jQuery的帮助下,当选择一个新的选择选项时,页面会改变。但是,当新页面呈现时,选择菜单中的默认选项仍然是“起始页面”(第一个)。根据页面(菜单)更改选定的下拉选项
我能做些什么来改变当前页面?
的header.php:
<div id="navigation" role="navigation">
wp_nav_menu(array(
'container' => false,
'menu_id' => 'nav',
'theme_location' => 'primary', // your theme location here
'walker' => new Walker_Nav_Menu_Dropdown(),
'items_wrap' => '<select>%3$s</select>',
'fallback_cb' => 'bp_dtheme_main_nav'
));
class Walker_Nav_Menu_Dropdown extends Walker_Nav_Menu{
function start_lvl(&$output, $depth){
$indent = str_repeat("\t", $depth);
}
function end_lvl(&$output, $depth){
$indent = str_repeat("\t", $depth);
}
function start_el(&$output, $item, $depth, $args){
$item->title = str_repeat(" - ", $depth).$item->title;
parent::start_el($output, $item, $depth, $args);
$href =! empty($item->url) ? ' value="' . esc_attr($item->url) .'"' : '#';
$output = str_replace('<li', '<option '.$href, $output);
}
function end_el(&$output, $item, $depth){
$output .= "</option>\n"; // replace closing </li> with the option tag
}
} ?>
</div>
的jQuery:
$("#navigation select").on('change', function() {
window.location = jq(this).find("option:selected").val();
});
谢谢!它工作,如果我改变===为==。显然,这个值和url在某种程度上稍有不同(即使他们在console.logging时看起来完全一样) – holyredbeard 2013-03-05 14:58:44
@holyredbeard对不起,'window.location'是一个对象。而不是使用'$(this).val()== window.location'并强制类型,使用'$(this).val()=== window.location.toString()'。我会编辑我的答案来反映这一点。 – 2013-03-06 11:45:15