Search code examples
javascriptjavaspringhref

Get the selected value from a springform:select and use it as a parameter href


I wanna know how can I get the selected value from

<springform:select id="novoPlastico" class="input" path="novoPlastico.codPlastico" value="${form.novoPlastico.codPlastico}">
                        <option value=-1>Selecione</option>
                    </springform:select>

And

<td><springform:select id="grupoAfinidade" class="input" path="grupoAfinidade.grp" value="${form.grupoAfinidade.grp}" onchange="carregarNovosPlasticos();">
                    <option value=-1>Selecione</option>
                </springform:select></td>       

And use these values ​​in the following href

  <a href="${pageContext.request.contextPath}/parametros/migrCanais.do?plastNovo=${
    --HERE I WANT THE FIRST SELECTED VALUE -- }&grp=${ -- HERE I WANT THE SECOND SELECTED VALUE --}" class="bt"><span>Go!</span></a>

EDIT1:

<script type="text/JavaScript" language="JavaScript" src="${pageContext.request.contextPath}/js/jquery-2.1.4.min.js"></script>

jQuery. noConflict();

jQuery("#btConfirma").on("click", function(e) {
    e.preventDefault();

    var value1 = jQuery("#novoPlastico").val();
    var value2 = jQuery("#grupoAfinidade").val();

    window.location.href = ctx + "/parametros/migrCanais.do?plastNovo=" + value1 + "&grp=" + value2;
});

<a id="btConfirma" class="bt" href="#"><span>Confirmar migração de canais de venda</span></a>

Solution

  • First add this inside your jsp head element:

    <script type="text/javascript">var ctx = "${pageContext.request.contextPath}"</script>
    

    After that try with following javascript code:

    $(".bt").on("click", function(e) {
        e.preventDefault();
    
        var value1 = $("#novoPlastico").val();
        var value2 = $("#grupoAfinidade").val();
    
        window.location.href = ctx + "/parametros/migrCanais.do?plastNovo=" + value1 + "&grp=" + value2;
    })