Load page on selection from dropdown form

前端 未结 5 1049
醉酒成梦
醉酒成梦 2020-12-08 03:14

I am trying to make a drop down menu that takes me to various webpages. Right now it is set up so you make your selection and then you hit a \"Go\" button and then it takes

相关标签:
5条回答
  • 2020-12-08 03:29

    Check out jQuery .change()

    <script>
       $('select.menu').change(function(e){
          // this function runs when a user selects an option from a <select> element
          window.location.href = $("select.menu option:selected").attr('value');
       });
    </script>
    
    0 讨论(0)
  • 2020-12-08 03:29

    Something like this might help - basically you just tie an onChange event to the select so that when a new option is selected it'll forward the location to the page.

    <p align="center">
    <form name="jump" class="center">
    <select name="menu" onchange="gotoPage(this)">
    <option value="#">Select an option</option>
    <option value="/link1.shtml">Link 1</option>
    <option value="/link2.shtml">Link 2</option>
    <option value="/link3.shtml">Link 3</option>
    </select>
    <input type="button" onClick="location=document.jump.menu.options[document.jump.menu.selectedIndex].value;" value="GO">
    </form>
    </p>
    
    <script type="text/javascript">
    function gotoPage(select){
        window.location = select.value;
    }
    </script>
    
    0 讨论(0)
  • 2020-12-08 03:33

    Try the following:

    <select onchange="location = this.options[this.selectedIndex].value;">
        <option>Please select</option>
        <option value="http://www.apple.com/">Apple</option>
        <option value="http://www.bbc.com">BBC</option>
        <option value="http://www.facebook.com">Facebook</option>
    </select>​
    

    What you were looking for was 'onchange' instead of 'onsubmit'

    0 讨论(0)
  • 2020-12-08 03:33

    I would recommend that you start using the javascript framework jQuery as it really will make your life much easier when it comes to javascript.

    When you have jQuery installed and setup in you web page you should be able to do something like this:

    <script type="text/javascript">
        $(document).ready(function() {
            $("#selection").change(function() {
                location = $("#selection option:selected").val();
            });
        });
    </script>
    
    <p align="center">
        <form name="jump" class="center">
            <select name="menu" id="selection>
                <option value="#">Select an option</option>
                <option value="/link1.shtml">Link 1</option>
                <option value="/link2.shtml">Link 2</option>
                <option value="/link3.shtml">Link 3</option>
            </select>
        </form>
    </p>
    
    0 讨论(0)
  • 2020-12-08 03:33

    Obviously super late to the party here, but since I was trying to figure out the same thing and was able to, I'll post how here.

    The other answers have you load the link when you change your select element option, but you originally had asked to do it with a button, after making your selection. This worked for me:

    <script type="text/javascript">
        $(document).ready(function() {
            var newUrl = "";
            $("#picksite").change(function() {
                $newUrl = $("#picksite option:selected").val();
            });
            $("#executelink").click(function() {
                location = $newUrl ;
            });
        });
    </script>
    
    <select id="picksite">
        <option value="">Pick A Website</option>
        <option value="http://google.com">Google</option>
        <option value="http://facebook.com">Facebook</option>
        <option value="http://twitter.com">Twitter</option>
        <option value="http://gmail.com">Gmail</option>
    </select>
    
    <button id="executelink">Go To Site</button>

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