简体   繁体   English

javascript下拉列表未及时加载

[英]javascript dropdown not loading in time

My problem is that I am trying to create a javascript menu but the script seems to be loading really slow and late so it's not working on the first seconds. 我的问题是我正在尝试创建一个javascript菜单,但是该脚本似乎加载得很慢而且很晚,因此在开始的几秒钟内无法正常工作。 Correct me if I am wrong. 如果我错了,请纠正我。 Second, I don't manage to make each column to work well and sometimes it's just linking to the same page, no matter which option I choose as well to another problem that there is no reaction to my choice, like I didn't do anything even if I really chose something from the menu. 其次,我无法使每个列都正常工作,有时它只是链接到同一页面,无论我选择哪个选项,以及另一个对我的选择没有反应的问题,就像我没有那样即使我真的从菜单中选择了一些东西

Do you know what can be the problem? 您知道可能是什么问题吗? Is it a bad idea to do it with JavaScript? 使用JavaScript这样做不是一个好主意吗? Would you make it with PHP? 你会用PHP做到吗? I also thing that the code is maybe too long. 我还认为代码可能太长。

This is the example for my problem - link 这是我的问题的示例- 链接

<div class="quick_width">
      <div class="quick_links"><img src="http://www.101greatgoals.com/wp-content/themes/tutorial/images/quick_links.jpg" width="102" height="37" alt=" "></div>
      <div class="green_bg">
        <div class="option_width">
          <div class="form_row_name_input1">
          <ul class="dropdownul">
<li id="categories">

<?php wp_dropdown_categories('show_option_none=Country'); ?>


<script type="text/javascript"><!--

var dropdown = document.getElementById("cat");

function onCatChange() {

if ( dropdown.options[dropdown.selectedIndex].value > 0 ) {

location.href = "<?php echo get_option('home');

?>/category/goals/"+dropdown.options[dropdown.selectedIndex].text.split(' ').join('-');

}

}

dropdown.onchange = onCatChange;

--></script>

</li>
</ul>
          </div>
          <div class="form_row_name_input1">
          <ul class="dropdownul">
<li id="categories">

<?php wp_dropdown_categories('taxonomy=teams&show_option_none=Teams&name=teamsmenu'); ?>


<script type="text/javascript"><!--

var dropdown = document.getElementById("teamsmenu");

function onCatChange() {

if ( dropdown.options[dropdown.selectedIndex].value > 0 ) {

location.href = "<?php echo get_option('home');

?>/?teams="+dropdown.options[dropdown.selectedIndex].text.split(' ').join('-');

}

}

dropdown.onchange = onCatChange;

--></script>

</li>
</ul>
          </div>

          <div class="form_row_name_input1">
          <ul class="dropdownul">
<li id="categories">

<?php wp_dropdown_categories('taxonomy=players&show_option_none=Players&name=playersmenu'); ?>


<script type="text/javascript"><!--

var dropdown = document.getElementById("playersmenu");

function onCatChange() {

if ( dropdown.options[dropdown.selectedIndex].value > 0 ) {

location.href = "<?php echo get_option('home');

?>/?players="+dropdown.options[dropdown.selectedIndex].text.split(' ').join('-');

}

}

dropdown.onchange = onCatChange;

--></script>

</li>
</ul>
          </div>

          <div class="form_row_name_input1">
          <ul class="dropdownul">
<li id="categories">

<?php wp_dropdown_categories('taxonomy=managers&show_option_none=Managers&name=managersmenu'); ?>


<script type="text/javascript"><!--

var dropdown = document.getElementById("managersmenu");

function onCatChange() {

if ( dropdown.options[dropdown.selectedIndex].value > 0 ) {

location.href = "<?php echo get_option('home');

?>/?managers="+dropdown.options[dropdown.selectedIndex].text.split(' ').join('-');

}

}

dropdown.onchange = onCatChange;

--></script>

</li>
</ul>
          </div>

          <div class="form_row_name_input1">
          <ul class="dropdownul">
<li id="categories">

<?php wp_dropdown_categories('taxonomy=clean_feeds&show_option_none=Other&name=othermenu'); ?>


<script type="text/javascript"><!--

var dropdown = document.getElementById("othermenu");

function onCatChange() {

if ( dropdown.options[dropdown.selectedIndex].value > 0 ) {

location.href = "<?php echo get_option('home');

?>/?clean_feeds="+dropdown.options[dropdown.selectedIndex].text.split(' ').join('-');

}

}

dropdown.onchange = onCatChange;

--></script>

</li>
</ul>
          </div>



        </div>
      </div>
      <div class="right_align"></div>
    </div>

In this case, I would definitely recommend creating the HTML output through PHP beforehand. 在这种情况下,我绝对建议您事先通过PHP创建HTML输出。 This way, when the page has loaded you can trigger your menu-script. 这样,在页面加载后,您可以触发菜单脚本。

The example provided in the WordPress reference for the wp_dropdown_categories() function has a cool example which should help you on your way. WordPress参考中提供的wp_dropdown_categories()函数示例包含一个很酷的示例,该示例可以为您提供帮助。 What it does is basically replace certain parts of the generated HTML. 它所做的基本上是替换生成的HTML的某些部分。 These replacements in your case would then be mainly in the generated URL. 在您的情况下,这些替换将主要在生成的URL中。 Example reproduced here for clarity: 为了清楚起见,此处复制了示例:

<li id="categories">
    <h2><?php _e('Posts by Category'); ?></h2>
    <form action="<?php bloginfo('url'); ?>/" method="get">
    <div>
    <?php
        $select = wp_dropdown_categories('show_option_none=Select category&show_count=1&orderby=name&echo=0');
        $select = preg_replace("#<select([^>]*)>#", "<select$1 onchange='return this.form.submit()'>", $select);
        echo $select;
    ?>
    <noscript><div><input type="submit" value="View" /></div></noscript>
    </div></form>
</li>

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

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