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

时间:2016-07-13 09:47:06

标签: javascript forms pardot

我对javascript很新,所以对某些人来说这可能很容易。我试图根据访问者是否选择选项" 0到120,000"来生成感谢信息。 AND "选项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";
    }
  }
}
&#13;
<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>
&#13;
&#13;
&#13;

3 个答案:

答案 0 :(得分: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>

答案 1 :(得分:0)

我建议使用此版本

  1. 您不应在名称上使用getElementById - 而是在ID上使用它
  2. 如果您不参加表格行动,则无需提交表格
  3. 无需循环以从选择
  4. 获取值
  5. 测试值而不是值
  6. 的索引

    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
    3. 时仍然使用ID而不是名称
    4. 请注意,JavaScript中的集合和数组从0开始 - 我认为您缺少“请选择”选项
    5. 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>

答案 2 :(得分: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" />