2014-09-21 32 views
3

我想根據選擇值更改表單操作。在選擇選項上更改表單操作

嗨,大家好,

<form name="store" id="store" method="post" action=""> 
<select name="storeID"> 
<option value="/stores/store6.php">6</option> 
<option value="/stores/store10.php">10</option> 
</select>     
</form> 

現在我想的表單操作,使用選擇選項的值。例如:

如果選擇選擇1,使用/stores/store6.php

+0

'' – Steve 2014-09-21 15:50:08

回答

9

可以使用onchange事件改變形式的行動

document.getElementById('store').storeID.onchange = function() { 
    var newaction = this.value; 
    document.getElementById('store').action = newaction; 
}; 

Here與代碼的jsfiddle。

+0

你的小提琴在Firefox中不起作用 – nematoth 2017-06-22 05:24:33

+0

@nematoth剛剛嘗試過它,它似乎在FF 54中正常工作。 – 2017-06-22 05:31:30

0

如下因素表單動作添加到選擇onchange功能

<select name="storeID" onchange='changeAction(this.value)'> 

和添加的JavaScript

function changeAction(val){ 
    document.getElementById('storeID').setAttribute('action', val); 
} 

這會在所選選項更改爲選定選項值後更改操作。

0

添加到您的選擇:

的onchange = 「changeAction(本)」

,這對您的JavaScript

changeAction = function(select){ 
    document.getElementById("store").action = select.value; 
} 
2
<form name="store" id="store" method="post" action="" id="FORM_ID" > 
    <select name="storeID"> 
     <option value="/stores/store6.php">6</option> 
     <option value="/stores/store10.php">10</option> 
    </select>     
</form> 

<script type="text/javascript"> 
document.getElementById('storeID').onchange = function(){ 
    document.getElementById('FORM_ID').action = '/'+this.value; 
} 
</script> 

檢查出來我希望它會工作..