Change form action on select option

前端 未结 4 745
死守一世寂寞
死守一世寂寞 2020-12-03 18:13

I want to change the form action based on a selection value.

Hi Guys,

相关标签:
4条回答
  • 2020-12-03 19:03
    <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 ..

    0 讨论(0)
  • 2020-12-03 19:04

    Add this to your select:

    onchange="changeAction(this)"

    and this to your javascript

    changeAction = function(select){
       document.getElementById("store").action = select.value;
    }
    
    0 讨论(0)
  • 2020-12-03 19:09

    You can use the onchange event to change the form's action

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

    Here is a jsfiddle with the code.

    0 讨论(0)
  • 2020-12-03 19:09

    Add to select onchange function

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

    and add to javascript

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

    This will change action after selected option is changed to selected option value.

    0 讨论(0)
提交回复
热议问题