最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Navigate to a page with selection on dropdown menu with JQuery - Stack Overflow

programmeradmin7浏览0评论

I am trying to navigate to a page on language selection. Here is my HTML code which I got from a template.

<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.

I am trying to navigate to a page on language selection. Here is my HTML code which I got from a template.

<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.

Share Improve this question edited Sep 23, 2014 at 19:11 Devin 7,7306 gold badges43 silver badges56 bronze badges asked Sep 23, 2014 at 19:08 Arif YILMAZArif YILMAZ 5,88629 gold badges114 silver badges197 bronze badges
Add a ment  | 

5 Answers 5

Reset to default 2

Well... here's a really simple example simply redirecting to page on an onchange event.

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./'+ value +'.html'; //or .php, etc. This will go to a page called en.html
    });

You can do smth like the following:

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:

$('.select-language').on('change', function(){
    location.href = $(this).val();
});

Below code will help you

  jquery:

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

If you want to redirect to www.mypage./en

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

评论列表(0)

  1. 暂无评论