Javascript表单重定向返回错误

时间:2017-08-15 18:46:02

标签: javascript html

我确定这必须是一个简单的修复,但我不确定出了什么问题。我正在寻找创建一个重定向的表单,具体取决于多个字段的选择。

我的编码如下:

<form onsubmit="return setAction()" method="post" action="">
<div class="row">
<label for="FormCtrl_1">Where are you going?</label>
<select id="FormCtrl_1" name="FormCtrl_1">
  <option value="Afghanistan">Afghanistan</option>
  <option value="Albania">Albania</option>
</select>
<select id="FormCtrl_2" name="FormCtrl_2">
  <option value="Business">Business</option>
  <option value="Pleasure">Plesure</option>
</select>
<select id="FormCtrl_3" name="FormCtrl_3">
  <option value="1">1 Day</option>
  <option value="2">2 Day</option>
</select>
</div>
<div class="row">
<input value="Submit" type="submit" />
</div>
</form>

使用js:

function setAction() {
var country = document.getElementById("FormCtrl_1").value;
var reason = document.getElementById("FormCtrl_2").value;
var days = document.getElementById("FormCtrl_3").value;

if (country === "Afghanistan" && reason === "Business" && days === "1") 
{
    window.location = "https://google.com";
}

if (country === "Afghanistan" && reason === "Business" && days === "2") 
{
    window.location = "https://facebook.com";
}

return false; }

0 个答案:

没有答案