URL直接取决于表单选择

时间:2014-06-24 11:30:15

标签: forms url redirect

我有一个简单的表单,我想将用户重定向到我网站中的特定网址,具体取决于他们选择的值。到目前为止,我有这个,但我不是指向4个网址中的一个,而是取而代之的是#34; http://mydomain.com/?ApplianceType=americano.html&sub=Submit"在我的地址栏中:

<form name="form1" method="get">
        <p>
          <label>
            <select name="ApplianceType" id="ApplianceType">
              <option value="americano.html">American Fridge Freezer</option>
              <option value="freezer.html">Freezer only</option>
              <option value="fridge.html">Fridge only</option>
              <option value="fridgefreezer.html">Fridge/Freezer</option>
            </select>
          </label>
          <label>
            <input type="submit" name="sub" id="sub" value="Submit">
          </label>
        </p>
      </form>

有人可以提供建议吗?

1 个答案:

答案 0 :(得分:0)

您可以更改HTML:

<form name="form1" id="yourForm" action="americano.html" method="get"> <!-- Added an ID to the form, and set the default value of `action` to 'americano.html'. -->
    <p>
      <label>
        <select name="ApplianceType" id="ApplianceType" onchange="setURL(this)"> <!-- Assigned the `select` to run the function on value change. -->
          <option value="americano.html">American Fridge Freezer</option>
          <option value="freezer.html">Freezer only</option>
          <option value="fridge.html">Fridge only</option>
          <option value="fridgefreezer.html">Fridge/Freezer</option>
        </select>
      </label>
      <label>
        <input type="submit" name="sub" id="sub" value="Submit">
      </label>
    </p>
</form>

然后使用以下Javascript函数:

<script>
    function setURL(selectedValue)
    {    
        var element = document.getElementById('yourForm'); // Change 'yourForm' to the actual ID you assign on your form.
        element.action = selectedValue.value;
    }
</script>

请注意代码中的注释。