简体   繁体   中英

Dropdown form with submit button

I have a dropdown with links in them, when I select the option it will go to the page, but I want to use a button instead but not sure how to change the javascript to make this happen:

   <select name="form" onchange="location = this.options[this.selectedIndex].value;"class="form-control">
          <option>Choose a Service</option>
          <option value="http://www.google.com">Option 1</option>
          <option value="http://www.google.com">Option 2</option>
          <option value="http://www.google.com">Option 3</option>
          <option value="http://www.google.com">Option 4</option>
          <option value="http://www.google.com">Option 5</option>
        </select>

Instead of putting location = this.options[this.selectedIndex].value in unChange , move it to a button onClick event:

<select id="dropdown" name="form" class="form-control">
  <option>Choose a Service</option>
  <option value="http://www.google.com">Option-1</option>
  <option value="http://www.google.com">Option-2</option>
  <option value="http://www.google.com">Option-3</option>
  <option value="http://www.google.com">Option-4</option>
  <option value="http://www.google.com">Option-5</option>
</select>

<input type="button" value="Go!" onClick='location = document.getElementById("dropdown").options[document.getElementById("dropdown").selectedIndex].value'>
   <select id="dd" name="form">
      <option>Choose a Service</option>
      <option value="http://www.google.com">Option 1</option>
      <option value="http://www.google.com">Option 2</option>
      <option value="http://www.google.com">Option 3</option>
      <option value="http://www.google.com">Option 4</option>
      <option value="http://www.google.com">Option 5</option>
    </select>

    <button onClick="launchPage" >

    <script>
      var launchPage = function() {
        var e = document.getElementById("dd");
        var url = e.options[e.selectedIndex].value;
        window.open(url);
      };
    </script>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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