Submit Button Action Depending If Field Matches Another Form

穿精又带淫゛_ 提交于 2019-12-13 02:05:56

问题


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:


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




回答2:


Use value and array for this.

if( ['AZ', 'AR', 'CA'].indexOf( document.getElementById('state').value) )

Or in jQuery you can use jQuery.inArray().

if( $.inArray($('#state').val(), ['AZ', 'AR', 'CA'] ) )


来源:https://stackoverflow.com/questions/35777024/submit-button-action-depending-if-field-matches-another-form

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!