使用 javascript 根据条件表单字段选择重定向感谢消息

Redirect thank you messages based on conditional form field selects using javascript

本文关键字:选择 重定向 感谢 消息 字段 表单 javascript 条件 使用      更新时间:2023-09-26

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

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

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

我建议改用这个版本

  1. 您不应该在名称上使用getElementById - 而是在ID上使用它
  2. 如果您不打算执行表单操作,则无需提交表单
  3. 无需循环即可从 SELECT 中获取值
  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. 提交时使用并返回假
  2. 使用
  3. document.getElementById 时仍使用 ID 而不是名称
  4. 请注意,在 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>

这是对我有用的:

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