简体   繁体   English

使用JQuery导航到下拉菜单上具有选择的页面

[英]Navigate to a page with selection on dropdown menu with JQuery

I am trying to navigate to a page on language selection. 我正试图导航到一个关于语言选择的页面。 Here is my HTML code which I got from a template. 这是我从模板获得的HTML代码。

<div class="change-language">
     <select class="select-language selectpicker">
          <option value="en">Türkçe</option>
          <option value="ge">English</option>
     </select>
</div>

How can I navigate to a page when a user selects a language? 当用户选择语言时,如何导航到页面? I have to do it with JQuery. 我必须使用JQuery。

Well... here's a really simple example simply redirecting to page on an onchange event. 好吧...这是一个非常简单的示例,仅在onchange事件上重定向到页面。

The page that it redirects to is simply the value of the dropdown and the file extension of the target file. 它重定向到的页面只是下拉列表的值和目标文件的文件扩展名。

$('.select-language').on('change',function(){
    var value = $(this).val();
    location.href = value +'.html'; //or .php, etc. This will go to a page called en.html
    });

More specifik target involving the protocol: 涉及该协议的更多特定目标:

$('.select-language').on('change',function(){
    var value = $(this).val();
    location.href = 'http://your-url.com/'+ value +'.html'; //or .php, etc. This will go to a page called en.html
    });

You can do smth like the following: 你可以像下面这样做:

HTML: HTML:

<div class="change-language">
     <select class="select-language selectpicker">
          <option value="page-tu.html">Türkçe</option>
          <option value="page-en.html">English</option>
     </select>
</div>

And JQuery: 和JQuery:

$('.select-language').on('change', function(){
    location.href = $(this).val();
});
$(".select-language").on("change", function() {
   //Will redirect to en.url.com, ge.url.com, etc...
   window.location.href = "http://" + this.value + ".url.com";          
});

Below code will help you 下面的代码将帮助您

  jquery:

   $(document).ready(function(){    
    $(".select-language").change(function(){      
     var page_url = "http://"+$(".select-language").val()+".com";    
     $(location).attr('href',page_url);
    });
   });

If you want to redirect to www.mypage.com/en 如果您想重定向到www.mypage.com/en

$(".select-language.selectpicker").on("change", function(){
    window.location.href = this.value;
});

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

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