Onchange open URL via select - jQuery

前端 未结 14 934
长情又很酷
长情又很酷 2020-11-27 10:15

What would be the best way to attach an event so on change of a select option a URL. Store the href in an attr and grab it on change?

相关标签:
14条回答
  • 2020-11-27 10:57

    You can use this simple code snippet using jQuery to redirect from a drop down menu.

    <select id="dynamic-select">
        <option value="" selected>Pick a Website</option>
        <option value="http://www.google.com/">Google</option>
        <option value="http://www.youtube.com/">YouTube</option>
        <option value="http://www.stackoverflow.com/">Stack Overflow</option>
    </select>
    
    <script>
        $('#dynamic-select').bind('change', function () { // bind change event to select
            var url = $(this).val(); // get selected value
            if (url != '') { // require a URL
                window.location = url; // redirect
            }
            return false;
        });
    </script>
    
    0 讨论(0)
  • 2020-11-27 11:01

    It is pretty simple, let's see a working example:

    <select id="dynamic_select">
      <option value="" selected>Pick a Website</option>
      <option value="http://www.google.com">Google</option>
      <option value="http://www.youtube.com">YouTube</option>
      <option value="https://www.gurustop.net">GuruStop.NET</option>
    </select>
    
    <script>
        $(function(){
          // bind change event to select
          $('#dynamic_select').on('change', function () {
              var url = $(this).val(); // get selected value
              if (url) { // require a URL
                  window.location = url; // redirect
              }
              return false;
          });
        });
    </script>
    

    $(function() {
      // bind change event to select
      $('#dynamic_select').on('change', function() {
        var url = $(this).val(); // get selected value
        if (url) { // require a URL
          window.location = url; // redirect
        }
        return false;
      });
    });
    <select id="dynamic_select">
      <option value="" selected>Pick a Website</option>
      <option value="http://www.google.com">Google</option>
      <option value="http://www.youtube.com">YouTube</option>
      <option value="https://www.gurustop.net">GuruStop.NET</option>
    </select>
    
    
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"
            ></script>

    .

    Remarks:

    • The question specifies jQuery already. So, I'm keeping other alternatives out of this.
    • In older versions of jQuery (< 1.7), you may want to replace on with bind.
    • This is extracted from JavaScript tips in Meligy’s Web Developers Newsletter.

    .

    0 讨论(0)
  • 2020-11-27 11:02

    I think this is the simplest way:

    <select onchange="if (this.value) window.location.href=this.value">
        <option value="">Pick one:</option>
        <option value="/foo">Foo</option>
        <option value="/bar">Bar</option>
    </select>
    
    0 讨论(0)
  • 2020-11-27 11:03

    JS Fiddle Example

    $('#userNav').change(function() {
    window.location = $(':selected',this).attr('href')
    });
    
    
    
    
    <select id="userNav">
    <option></option>
    <option href="http://google.com">Goolge</option>
    <option href="http://duckduckgo.com">Go Go duck</option>
    </select>
    

    This works for the href in an option that is selected

    0 讨论(0)
  • 2020-11-27 11:03

    Another way:

        <script type="text/javascript">
            function change_url(val) {
                window.location=val;
            }
        </script>            
        <select style="width:130px;" onchange="change_url(this.value);">
                <option value="http://www.url1.com">Option 1</option>
                <option value="http://www.url2.com">Option 2</option>
         </select>
    
    0 讨论(0)
  • 2020-11-27 11:06

    Try this code its working Firefox, Chrome, IE

    <select onchange="this.options[this.selectedIndex].value && (window.location = this.options[this.selectedIndex].value);">
    <option value="" selected>---Select---</option>
    <option value="https://www.google.com">Google</option>
    <option value="https://www.google.com">Google</option>
    <option value="https://www.google.com">Google</option>
    <option value="https://www.google.com">Google</option>
    

    0 讨论(0)
提交回复
热议问题