Javascript - 自定义网址取决于所选选项

时间:2015-01-14 15:00:29

标签: javascript url select parameters

<form name="myform" action="#" method="post">
        <div class="col-md-4">
            <div class="col-md-4 desc">Naziv marke: </div>
            <div class="col-md-8">
                <select class="form-control" id="brandovi">
                    <option value="AllBrands" selected>Sve marke</option>
                    <option value="Dunlop">Dunlop</option>  
                    <option value="Fulda">Fulda</option>    
                    <option value="Goodyear">Goodyear</option>  
                    <option value="Sava">Sava</option>          
                </select>
            </div>
        </div>
        <div class="col-md-2">
            <div class="btn-group">
                <input type="radio" autocomplete="off" name="season" value="Summer"> <img width="20" src="img/ikone/summer.png" />
                <input type="radio" autocomplete="off" name="season" value="AllSeason"> <img width="20" src="img/ikone/allSeason.png" />
                <input type="radio" autocomplete="off" name="season" value="Winter"> <img width="20" src="img/ikone/winter.png" />
            </div>
        </div>
        <div class="col-md-4">
            <div class="col-md-4 desc">Tip vozila:</div>
            <div class="col-md-8">
                <select class="form-control">
                    <option value="AllTypes"> Sve vrste </option>
                    <option value="fourxfour"> 4x4</option>
                </select>
            </div>
        </div>
        <div class="col-md-2 text-right">
            <input type="submit" name="submit" value="Traži"/>
        </div>
    </form>

我在HTML中有这个,所以我需要javascript代码,根据选定的值输出自定义URL

Url示例

http://link.com/?brandovi=sava&sezona=zima&vrstaVozila=4x4

我尝试了几个JS代码,但我无法做到。 请帮忙。

1 个答案:

答案 0 :(得分:0)

我解决了问题:D

这是解决方案

<script type="text/javascript">
function submitUrl(form){
    var brandovi = document.getElementById("brandovi");
    var brand = brandovi.options[brandovi.selectedIndex].value;
    var vrsteVozila = document.getElementById("vrsteVozila");
    var vrsta = vrsteVozila.options[vrsteVozila.selectedIndex].value;

    var radios = document.getElementsByName('season');

    for (var i = 0, length = radios.length; i < length; i++) {
    if (radios[i].checked) {
    var sezona = radios[i].value;
}
    else{
        var sezona = '';
    }
}  
    window.location = 'http://localhost/wordpress/' +  '?brandovi=' + brand + '&sezona=' + sezona + '&vrstaVozila=' + vrsta;
}