使用 javascript 根据条件表单字段选择重定向感谢消息
Redirect thank you messages based on conditional form field selects using javascript
我对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>
我建议改用这个版本
- 您不应该在名称上使用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" />
要修复您的 POSTED 版本,您需要按照我在编写更好版本之前的第一条评论中的建议进行操作:
- 提交时使用并返回假 使用
- document.getElementById 时仍使用 ID 而不是名称
- 请注意,在 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" />
相关文章:
- 在选择时自动重定向
- 当用户在选择框中选择选项时,使用window.location.href重定向用户只会更改导航器中URL的最后一位
- 基于单选/选择选项重定向
- 根据选择选项元素将表单重定向到不同的URL,并在电子邮件中获取表单详细信息
- 如何根据选择下拉列表从表单重定向到 URL
- 链接内的内容可编辑块,如何防止重定向,但让文本选择插入符号位置更改
- 选择其中一个单选按钮,然后单击“提交”,重定向其他页面
- 重定向后选择选项
- 选择选项“重定向获取值”和“在重定向页面上显示”
- 如何基于两个下拉菜单选择进行页面重定向
- 如何让我的重定向脚本添加一个随机后缀来选择网站
- 在选择标记中的 ng 更改上重定向
- 如何根据 html 中的选项/选择重定向用户(单击按钮后)
- 要重定向的页面取决于多个“选择”选项
- 在jQuery Mobile中更改大选择菜单时重定向
- Javascript:根据单选按钮选择重定向到新页面
- 使用HTML下拉选择重定向
- 如何根据表单选择重定向人员
- 根据选择重定向到新的网页
- 使用 javascript 根据条件表单字段选择重定向感谢消息