使用 javascript 根据条件表单字段选择重定向感谢消息
Redirect thank you messages based on conditional form field selects using javascript
我是 javascript 的新手,所以这对某些人来说可能很容易。我正在尝试根据访问者是否选择选项“0 到 120,000”AND"option 0 to 6 months" 生成感谢消息。非常感谢任何帮助。
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>
我建议改用这个版本
- 您不应在名称上使用 getElementById - 而是在 ID 上使用它
- 如果您不打算执行表单操作,则无需提交表单
- 无需循环从 select
中获取值
- 测试值而不是值的索引
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" />
要修复您发布的版本,您需要按照我在编写更好的版本之前发表的第一条评论中的建议进行操作:
- 使用 onsubmit 和 return false
- 使用document.getElementById
时仍然使用ID而不是名称
- 请注意,集合和数组在 JavaScript 中从 0 开始 - 我认为您缺少 "please select" 选项
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" />
我是 javascript 的新手,所以这对某些人来说可能很容易。我正在尝试根据访问者是否选择选项“0 到 120,000”AND"option 0 to 6 months" 生成感谢消息。非常感谢任何帮助。
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>
我建议改用这个版本
- 您不应在名称上使用 getElementById - 而是在 ID 上使用它
- 如果您不打算执行表单操作,则无需提交表单
- 无需循环从 select 中获取值
- 测试值而不是值的索引
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" />
要修复您发布的版本,您需要按照我在编写更好的版本之前发表的第一条评论中的建议进行操作:
- 使用 onsubmit 和 return false
- 使用document.getElementById 时仍然使用ID而不是名称
- 请注意,集合和数组在 JavaScript 中从 0 开始 - 我认为您缺少 "please select" 选项
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" />