duan62819774 2013-11-05 08:52
浏览 51
已采纳

我想将dropdown.selectedIndex更改为“text”而不是当前的“value”

I have a javascript dropdown on website, here is the code-

           <?php
                function get_request_uri_without_page() {
                    $pageURL = 'http';
                 if ($_SERVER["HTTPS"] == "on") {$pageURL .= "s";}
                 $pageURL .= "://";
                 if ($_SERVER["SERVER_PORT"] != "80") {
                  $pageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
                 } else {
                  $pageURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
                 }
                    $request = explode('/page/',$pageURL);
                    $request = explode('?', $request[0]);
                    return $request[0];
                }
            ?>
            <form action="<?php echo get_request_uri_without_page(); ?>" method="get" id="catselect" class="form-archive-140">
                                        <!-- DROP DOWN HERE -->
                <?php global $jtCatId; $jtCatId = true; wp_dropdown_categories('show_count=1&hierarchical=1&child_of=140&class=selectmenu&id=cat&show_option_none=Please Choose...'); ?>
                <script type="text/javascript">
                   <!--
                    var dropdown = document.getElementById("cat");
                    function onCatChange() {
                        if ( dropdown.options[dropdown.selectedIndex].value > 0 ) {
                            location.href = "<?php echo get_request_uri_without_page(); ?>?cat="+dropdown.options[dropdown.selectedIndex].value;
                        }
                    }
                    dropdown.onchange = onCatChange;
                    -->
                </script>

The output of this dropdown is a list of posts filtered by location, however the URL structure is as follows-

thewebsite/themaincategory?cat=141

I would like to replace the category id at the end with the category name, when I replace dropdown.selectedIndex to "text" the dropdown does not work, it doesn't give an error it just doesn't do anything.

Do I need to do a rewrite in the htaccess or is their a way to get the above code working.

I would like to apologise in advance if this is a stupid question, I am new to javascript and just trying to find my way.

Here is a working example of the above mentioned-

http://www.theweddingdirectory.co.za/professional-wedding-photographers

  • 写回答

1条回答 默认 最新

  • dongsu3138 2013-11-05 08:59
    关注

    try something like this

      var cat= document.getElementById("cat");
      var text = cat.options[cat.selectedIndex].text;
    

    EDITED CODE

            function onCatChange() {
                 if ( dropdown.options[dropdown.selectedIndex].value > 0 ) {
                     location.href = "<?php echo get_request_uri_without_page(); ?>?cat="+dropdown.options[dropdown.selectedIndex].text.trim();
                 }
             }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 算法题:数的划分,用记忆化DFS做WA求调
  • ¥15 chatglm-6b应用到django项目中,模型加载失败
  • ¥15 武汉岩海低应变分析软件,导数据库里不显示波形图
  • ¥15 CreateBitmapFromWicBitmap内存释放问题。
  • ¥30 win c++ socket
  • ¥30 CanMv K210开发板实现功能
  • ¥15 C# datagridview 栏位进度
  • ¥15 vue3页面el-table页面数据过多
  • ¥100 vue3中融入gRPC-web
  • ¥15 kali环境运行volatility分析android内存文件,缺profile