2016-03-03 41 views
1

我有一個頁面有兩種形式,但現在只顯示一個。想法是,如果消費者從下拉菜單中選擇某個狀態,那麼當他們點擊提交按鈕時,它將重定向到感謝頁面或第二個表單。提交按鈕動作取決於如果字段匹配另一種形式

這裏是我的形式:

<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> 

我敢肯定,我在這裏的某個地方犯了一個錯誤,但我似乎無法弄清楚。當消費者點擊提交按鈕時,它會將其重定向到感謝頁面或回到表單。

我已經看過每個單獨狀態都顯示自己的if/else語句的例子,但是我希望避免這種情況,因爲它會使我的文檔更長。

回答

0

1. =用於轉讓不作比較使用==代替它。

2.也將你的情況分成三部分,如下圖。

3.You在你的病情忘記''也圍繞state: -

if(document.getElementById('state').value == 'AZ' || document.getElementById('state').value == 'AR' || document.getElementById('state').value == 'CA'){.......}else{.....} 

或更有效的方式來做到這一點: -

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

4.Instead的location.href使用window.location.href

+0

Phew!你爲我節省了很多時間 - 非常感謝你! –

+0

歡迎@MikaelaS。 –

1

使用value和數組。

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

或者在jQuery中,您可以使用jQuery.inArray()

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

你忘了你的第一個案例中的''' –

+0

@Anant哦。謝了哥們。修復。 – RRK

+0

不客氣! –