2

我对javascript很陌生,所以这对某些人来说可能很容易。我正在尝试根据访问者是否选择选项“0 到 120,000”“选项 0 到 6 个月”来生成感谢信息。对此的任何帮助将不胜感激。

function redirect() {
  var businessrev = document.getElementById("annual_business_revenue");
  var time = document.getElementById("Time")
  for (var i = 0; i < selections.options.length; i++) {
    if ((businessrev.options[i].selected == 1) && (time.options[i].selected == 1)) {
      location.href = "http://www.bing.com";
    } else {
      location.href = "http://www.google.com";
    }
  }
}
<form action="javascript:redirect();">
  <select name="annual_business_revenue">
    <option value="revenue1">0 to 120,000</option>
    <option value="revenue2">NOT 0 to 120,000</option>
  </select>
  <select name="Time">
    <option value="time1">0 to 6months</option>
    <option value="time2">NOT 0 to 6months</option>
  </select>
  <input type="submit" value="Submit" />
</form>

4

3 回答 3

0

用这个。希望它会有所帮助。

<!DOCTYPE html>
<html>
<body>
<script type="text/javascript">
function abc() {
  var businessrev = document.getElementById("annual_business_revenue");
  var time = document.getElementById("Time");

  if((businessrev.selectedIndex=="0") && (time.selectedIndex =="0"))
         location.href = "http://www.bing.com";
 else
  location.href = "http://www.w3schools.com/";

return false;
}
</script>

<form onsubmit="return abc()">
  <select id="annual_business_revenue">
    <option value="revenue1">0 to 120,000</option>
    <option value="revenue2">NOT 0 to 120,000</option>
  </select>
  <select id="Time">
    <option value="time1">0 to 6months</option>
    <option value="time2">NOT 0 to 6months</option>
  </select>
  <input type="submit" value="Submit" />
</form>
</body>
</html>
于 2016-07-13T09:53:14.543 回答
0

我建议改用这个版本

  1. 您不应该在名称上使用 getElementById - 而是在 ID 上使用它
  2. 如果您不进行表单操作,则无需提交表单
  3. 无需循环从选择中获取值
  4. 测试值而不是值的索引

window.onload=function() {
  document.getElementById("goBut").onclick=function() {
    var businessrev = document.getElementById("annual_business_revenue").value;
    var time = document.getElementById("Time").value;
    if (businessrev=="revenue1" && time=="time1") {
        alert("Great choice");
        location.href = "http://www.bing.com";
    } else {
        location.href = "http://www.google.com";
    }
  }
}
<select id="annual_business_revenue">
    <option value="revenue1">0 to 120,000</option>
    <option value="revenue2">NOT 0 to 120,000</option>
  </select>
  <select id="Time">
    <option value="time1">0 to 6months</option>
    <option value="time2">NOT 0 to 6months</option>
  </select>
  <input id="goBut" type="button" value="Submit" />

要修复您的 POSTED 版本,您需要按照我在编写更好版本之前所做的第一条评论中的建议进行操作:

  1. 使用 onsubmit 并返回 false
  2. 使用 document.getElementById 时仍然使用 ID 而不是名称
  3. 请注意,集合和数组在 JavaScript 中从 0 开始 - 我认为您缺少“请选择”选项

function redirect() {
  var businessrev = document.getElementById("annual_business_revenue"),
      time = document.getElementById("Time");

  if (businessrev.selectedIndex == 0 && time.selectedIndex == 0) {
      alert("Good choice");
      location.href = "http://www.bing.com";
  } else {
      location.href = "http://www.google.com";
  }
  return false; // cancel submit
}
<form onsubmit="return redirect();">
  <select id="annual_business_revenue">
    <option value="revenue1">0 to 120,000</option>
    <option value="revenue2">NOT 0 to 120,000</option>
  </select>
  <select id="Time">
    <option value="time1">0 to 6months</option>
    <option value="time2">NOT 0 to 6months</option>
  </select>
  <input type="submit" value="Submit" />
</form>

使用您的表格:

window.onload=function() {
  document.getElementById("pardot-form").onsubmit=function() {
    var businessrev = this.elements[1], // second form element
        time = this.elements[2]; // third
    if (businessrev.selectedIndex == 1 && time.selectedIndex == 1) { // first choice
      alert("Good choice");
    }
  }
}
<form id="pardot-form">
  <input type="text" name="emial"/>
  <select>
    <option value="">Please select</option>
    <option value="revenue1">0 to 120,000</option>
    <option value="revenue2">NOT 0 to 120,000</option>
  </select>
  <select>
    <option value="">Please select</option>
    <option value="time1">0 to 6months</option>
    <option value="time2">NOT 0 to 6months</option>
  </select>
  <input type="submit" value="Submit" />
</form>

于 2016-07-13T09:54:23.637 回答
0

这对我有用:

window.onload=function(){
var annualRevenue = 'annual_business_revenue';	
var email = 'email';
var timeInBusiness = 'Time';

if((timeInBusiness == '0 to 6months' || annualRevenue == '0 to 120,000' )){
document.location='http://google.com';
}else{
document.location='http://bing.com';
}	
};
<select id="annual_business_revenue">
    <option value="revenue1">0 to 120,000</option>
    <option value="revenue2">NOT 0 to 120,000</option>
  </select>
  <select id="Time">
    <option value="time1">0 to 6months</option>
    <option value="time2">NOT 0 to 6months</option>
  </select>
  <input id="goBut" type="button" value="Submit" />

于 2016-07-21T11:50:17.093 回答