提交按钮操作取决于字段是否与另一个表单匹配 [英] Submit Button Action Depending If Field Matches Another Form
问题描述
我有一页有两种表格,但现在只显示一种。这个想法是,如果消费者从下拉菜单中选择了某个状态,那么当它们点击提交按钮时,它将重定向到感谢页面或第二个表单。
以下是我的表单:
< form action =capture_redirect.phpmethod =post>
< input type =submitid =submitvalue =Check Eligibility>
Javascript:
< script type =text / javascript>
函数提交{
if(document.getElementById(state)='AZ','AR','CA')
{
location.href =home.php ;
}
else
{
location.href =capture_redirect.php;
}
}
< / script>
我确定我在这里犯了一个错误,但我似乎无法弄清楚。现在,当消费者点击提交按钮时,它会将他们重定向到感谢页面或回到表单。
我看到了每个单独状态都显示的示例用它自己的if / else语句,但我希望避免这种情况,因为它会使我的文档更长。
1. =
==
而不是它。
2.也将你的情况分成三部分,如下图。
3.在您的情况下,您也忘记了'<
,大约州
: -
if(document.getElementById('state')。value =='AZ'|| document.getElementById('状态')。value =='AR'|| document.getElementById('state')。value =='CA'){.......} else {.....}
或者更有效的方法: -
pre > if(['AZ','AR','CA']。indexOf(document.getElementById('state')。value))
pre>
4.替代 location.href
使用 window.location.href
I have one page with two forms, but only one is shown for now. The idea being is if the consumer selects a certain state from the drop down menu, it will either redirect them to a thank you page or the second form when they click on the submit button.
Here is my form:
<form action="capture_redirect.php" method="post">
<select placeholder="State" name="_State" id="state" required/>
<input type="submit" id="submit" value="Check Eligibility">
Javascript:
<script type="text/javascript">
function submit {
if(document.getElementById(state) = 'AZ', 'AR', 'CA')
{
location.href = "home.php";
}
else
{
location.href = "capture_redirect.php";
}
}
</script>
I'm sure I made an error somewhere here but I can't seem to figure it out. Right now when the consumer clicks on the submit button it either redirects them to the thank you page or back on the form.
I have seen examples where each indivual state is displayed with it's own if/else statements, but I'm hoping to avoid that since it would make my document much longer.
1.=
is used for assignment not for comparison so use ==
instead of it.
2.Also break your condition into three parts like below.
3.You forgot ''
also, around state
in your condition:-
if(document.getElementById('state').value == 'AZ' || document.getElementById('state').value == 'AR' || document.getElementById('state').value == 'CA'){.......}else{.....}
Or more efficient way to do it:-
if( ['AZ', 'AR', 'CA'].indexOf( document.getElementById('state').value) )
4.Instead of location.href
use window.location.href
这篇关于提交按钮操作取决于字段是否与另一个表单匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!