下拉表单提交按钮

时间:2013-11-24 23:40:21

标签: javascript html forms

我有一个带有链接的下拉列表,当我选择它会进入页面的选项时,但我想使用一个按钮而不是确定如何更改javascript以实现此目的:

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

2 个答案:

答案 0 :(得分:1)

不要将location = this.options[this.selectedIndex].value放入unChange,而是将其移至按钮onClick

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

答案 1 :(得分:0)

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