Onchange open URL via select - jQuery

前端 未结 14 959
长情又很酷
长情又很酷 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:41

    I believe the simplest way to redefine a location inside select tab is as follows:

    <select onchange="location = this.value;">
         <option value="https://www.google.com/">Home</option>
         <option value="https://www.bing.com">Contact</option>
         <option value="mypets.php">Sitemap</option>
    </select>
    
    0 讨论(0)
  • 2020-11-27 10:41

    Super easy way is as following. No need to create a function.

    <select onchange="window.location = this.options[this.selectedIndex].value">
        <option value="">Switch Language</option>
        <option value="{{ url('/en') }}">English</option>
        <option value="{{ url('/ps') }}">پښتو</option>
        <option value="{{ url('/fa') }}">دری</option>
    </select>
    
    0 讨论(0)
  • 2020-11-27 10:47

    Here's how i'd do it

    <select id="urlSelect" onchange="window.location = jQuery('#urlSelect option:selected').val();">
     <option value="http://www.yadayadayada.com">Great Site</option>
     <option value="http://www.stackoverflow.com">Better Site</option>
    </select>
    
    0 讨论(0)
  • 2020-11-27 10:48
    <select name="xx" class="xxx" onchange="_name(this.options[this.selectedIndex].value,this.options[this.selectedIndex].getAttribute('rel'))">
      <option value="x" rel="xy">aa</option>
      <option value="xxx" rel="xyy">bb</option>
    </select>
    
    
    //for javascript
    function _name(value,rel) {
    alert(value+"-"+rel);
    }
    
    0 讨论(0)
  • 2020-11-27 10:53

    Try this code its working perfect

    <script>
    $(function() {
    
        $("#submit").hide();
    
        $("#page-changer select").change(function() {
            window.location = $("#page-changer select option:selected").val();
        })
    
    });
    </script>
    
    <?php
        if (isset($_POST['nav'])) {
             header("Location: $_POST[nav]");
        }
    ?>
    <form id="page-changer" action="" method="post">
        <select name="nav">
            <option value="">Go to page...</option>
            <option value="http://css-tricks.com/">CSS-Tricks</option>
            <option value="http://digwp.com/">Digging Into WordPress</option>
            <option value="http://quotesondesign.com/">Quotes on Design</option>
        </select>
        <input type="submit" value="Go" id="submit" />
    </form>
    
    0 讨论(0)
  • 2020-11-27 10:57

    Sorry, but there's to much coding going on here ...

    I'll give the simplest one to you for free. I invented it back in 2005, although the javascript source now says it was their staff who came up with it - more than a year later!

    Anyway, here it is, no javascript !!!

    <!-- Paste this code into the BODY section of your HTML document  -->
    <select size="1" name="jumpit" onchange="document.location.href=this.value"> 
    <option selected value="">Make a Selection</option>
    <option value="http://www.javascriptsource.com/">The JavaScript Source</option>
    <option value="http://www.javascript.com">JavaScript.com</option>
    <option value="http://www.webdeveloper.com/forum/forumdisplay.php?f=3">JavaScript Forums</option>
    <option value="http://www.scriptsearch.com/">Script Search</option>
    <option value="http://www.webreference.com/programming/javascript/diaries/">The JavaScript Diaries</option>
    </select> 
    

    Just type in any URL you like, or a relative URL (to the pages location on server), it will always work.

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