简体   繁体   English

在选择选项上更改表单操作

[英]Change form action on select option

I want to change the form action based on a selection value.我想根据选择值更改表单操作。

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

Now I want the form action, to use the select option value.现在我想要表单操作,使用选择选项值。 For example:例如:

If Selection 1 is selected, use the folowing form action /stores/store6.php如果选择了选择 1,请使用以下表单操作 /stores/store6.php

You can use the onchange event to change the form's action您可以使用onchange事件来更改表单的操作

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

Here is a jsfiddle with the code.是一个带有代码的jsfiddle。

Add to select onchange function添加选择onchange功能

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

and add to javascript并添加到 javascript

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

This will change action after selected option is changed to selected option value.这将在选定选项更改为选定选项值后更改操作。

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

check it out i wish it will work ..检查一下,我希望它会起作用..

Add this to your select:将此添加到您的选择中:

onchange="changeAction(this)" onchange="changeAction(this)"

and this to your javascript这给你的 javascript

changeAction = function(select){
   document.getElementById("store").action = select.value;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM