Take the value of select

Asked

Viewed 19,969 times

3

My Select is like this:

<select class="input-medium ajuste" id="sel_mes">
  <option value="09/03/2014">Março - 2014</option>
  <option value="09/04/2014">Abril - 2014</option>
  <option value="09/05/2014">Maio - 2014</option>
  <option value="09/06/2014">Junho - 2014</option>
  <option value="09/12/2015">Dezembro - 2015</option>
  <option value="09/01/2016">Janeiro - 2016</option>
</select>

I want to get the value, which is the date 09/04/2014 and not the text Abril - 2014

I’m using this:

var ValorA = $("#sel_mes").val();

I’ve tried this one too

var ValorA = $("#sel_mes option:selected").val();

But both return Abril - 2014

Colote via Jquery Thus

        function montaMeses(data){
        $.ajax({
           async: false,     
           type: "POST",
           url: "../execs/proc_fluxocaixa.php",
           data: {d_acao: "meses", c_data: data},
           dataType: "json",
           success: function(ret_dados){                         
               $("#sel_mes").html(ret_dados.meses);
           }                               
        });            
    }

IN PHP

function MesExt($mes){
    if ($mes == 1) {return "Janeiro";};
    if ($mes == 2) {return "Fevereiro";};
    if ($mes == 3) {return utf8_encode("Março");};
    if ($mes == 4) {return "Abril";};
    if ($mes == 5) {return "Maio";};
    if ($mes == 6) {return "Junho";};
    if ($mes == 7) {return "Julho";};
    if ($mes == 8) {return "Agosto";};
    if ($mes == 9) {return "Setembro";};
    if ($mes == 10){return "Outubro";};
    if ($mes == 11){return "Novembro";};
    if ($mes == 12){return "Dezembro";};
}

if($acao == "meses"){
    $optMeses = "";
    $atual = $_POST["c_data"];
    $Data = explode("/", $atual);

    $a1 = explode("/", $Sv->somar_data($atual, 0, 12, 0, false)); $MesExtenso = MesExt($a1[1]); $AnoExtenso = $a1[2]; $optMeses .= "<option value='" . $Sv->somar_data($atual, 0, 12, 0, false) . "'>$MesExtenso - $AnoExtenso</option>";
    $a1 = explode("/", $Sv->somar_data($atual, 0, 11, 0, false)); $MesExtenso = MesExt($a1[1]); $AnoExtenso = $a1[2]; $optMeses .= "<option value='" . $Sv->somar_data($atual, 0, 11, 0, false) . "'>$MesExtenso - $AnoExtenso</option>";
    $a1 = explode("/", $Sv->somar_data($atual, 0, 10, 0, false)); $MesExtenso = MesExt($a1[1]); $AnoExtenso = $a1[2]; $optMeses .= "<option value='" . $Sv->somar_data($atual, 0, 10, 0, false) . "'>$MesExtenso - $AnoExtenso</option>";
    $a1 = explode("/", $Sv->somar_data($atual, 0, 9,  0, false)); $MesExtenso = MesExt($a1[1]); $AnoExtenso = $a1[2]; $optMeses .= "<option value='" . $Sv->somar_data($atual, 0, 9,  0, false) . "'>$MesExtenso - $AnoExtenso</option>";
    $a1 = explode("/", $Sv->somar_data($atual, 0, 8,  0, false)); $MesExtenso = MesExt($a1[1]); $AnoExtenso = $a1[2]; $optMeses .= "<option value='" . $Sv->somar_data($atual, 0, 8,  0, false) . "'>$MesExtenso - $AnoExtenso</option>";
    $a1 = explode("/", $Sv->somar_data($atual, 0, 7,  0, false)); $MesExtenso = MesExt($a1[1]); $AnoExtenso = $a1[2]; $optMeses .= "<option value='" . $Sv->somar_data($atual, 0, 7,  0, false) . "'>$MesExtenso - $AnoExtenso</option>";
    $a1 = explode("/", $Sv->somar_data($atual, 0, 6,  0, false)); $MesExtenso = MesExt($a1[1]); $AnoExtenso = $a1[2]; $optMeses .= "<option value='" . $Sv->somar_data($atual, 0, 6,  0, false) . "'>$MesExtenso - $AnoExtenso</option>";
    $a1 = explode("/", $Sv->somar_data($atual, 0, 5,  0, false)); $MesExtenso = MesExt($a1[1]); $AnoExtenso = $a1[2]; $optMeses .= "<option value='" . $Sv->somar_data($atual, 0, 5,  0, false) . "'>$MesExtenso - $AnoExtenso</option>";
    $a1 = explode("/", $Sv->somar_data($atual, 0, 4,  0, false)); $MesExtenso = MesExt($a1[1]); $AnoExtenso = $a1[2]; $optMeses .= "<option value='" . $Sv->somar_data($atual, 0, 4,  0, false) . "'>$MesExtenso - $AnoExtenso</option>";
    $a1 = explode("/", $Sv->somar_data($atual, 0, 3,  0, false)); $MesExtenso = MesExt($a1[1]); $AnoExtenso = $a1[2]; $optMeses .= "<option value='" . $Sv->somar_data($atual, 0, 3,  0, false) . "'>$MesExtenso - $AnoExtenso</option>";
    $a1 = explode("/", $Sv->somar_data($atual, 0, 2,  0, false)); $MesExtenso = MesExt($a1[1]); $AnoExtenso = $a1[2]; $optMeses .= "<option value='" . $Sv->somar_data($atual, 0, 2,  0, false) . "'>$MesExtenso - $AnoExtenso</option>";
    $a1 = explode("/", $Sv->somar_data($atual, 0, 1,  0, false)); $MesExtenso = MesExt($a1[1]); $AnoExtenso = $a1[2]; $optMeses .= "<option value='" . $Sv->somar_data($atual, 0, 1,  0, false) . "'>$MesExtenso - $AnoExtenso</option>";

    $a1 = explode("/", $atual); $MesExtenso = MesExt($a1[1]); $AnoExtenso = $a1[2]; $optMeses .= "<option value'$atual' selected>$MesExtenso - $AnoExtenso</option>";

    $a1 = explode("/", $Sv->somar_data($atual, 0, 1,   0, true)); $MesExtenso = MesExt($a1[1]); $AnoExtenso = $a1[2]; $optMeses .= "<option value='" . $Sv->somar_data($atual, 0, 1,  0, true) . "'>$MesExtenso - $AnoExtenso</option>";
    $a1 = explode("/", $Sv->somar_data($atual, 0, 2,   0, true)); $MesExtenso = MesExt($a1[1]); $AnoExtenso = $a1[2]; $optMeses .= "<option value='" . $Sv->somar_data($atual, 0, 2,  0, true) . "'>$MesExtenso - $AnoExtenso</option>";
    $a1 = explode("/", $Sv->somar_data($atual, 0, 3,   0, true)); $MesExtenso = MesExt($a1[1]); $AnoExtenso = $a1[2]; $optMeses .= "<option value='" . $Sv->somar_data($atual, 0, 3,  0, true) . "'>$MesExtenso - $AnoExtenso</option>";
    $a1 = explode("/", $Sv->somar_data($atual, 0, 4,   0, true)); $MesExtenso = MesExt($a1[1]); $AnoExtenso = $a1[2]; $optMeses .= "<option value='" . $Sv->somar_data($atual, 0, 4,  0, true) . "'>$MesExtenso - $AnoExtenso</option>";
    $a1 = explode("/", $Sv->somar_data($atual, 0, 5,   0, true)); $MesExtenso = MesExt($a1[1]); $AnoExtenso = $a1[2]; $optMeses .= "<option value='" . $Sv->somar_data($atual, 0, 5,  0, true) . "'>$MesExtenso - $AnoExtenso</option>";
    $a1 = explode("/", $Sv->somar_data($atual, 0, 6,   0, true)); $MesExtenso = MesExt($a1[1]); $AnoExtenso = $a1[2]; $optMeses .= "<option value='" . $Sv->somar_data($atual, 0, 6,  0, true) . "'>$MesExtenso - $AnoExtenso</option>";
    $a1 = explode("/", $Sv->somar_data($atual, 0, 7,   0, true)); $MesExtenso = MesExt($a1[1]); $AnoExtenso = $a1[2]; $optMeses .= "<option value='" . $Sv->somar_data($atual, 0, 7,  0, true) . "'>$MesExtenso - $AnoExtenso</option>";
    $a1 = explode("/", $Sv->somar_data($atual, 0, 8,   0, true)); $MesExtenso = MesExt($a1[1]); $AnoExtenso = $a1[2]; $optMeses .= "<option value='" . $Sv->somar_data($atual, 0, 8,  0, true) . "'>$MesExtenso - $AnoExtenso</option>";
    $a1 = explode("/", $Sv->somar_data($atual, 0, 9,   0, true)); $MesExtenso = MesExt($a1[1]); $AnoExtenso = $a1[2]; $optMeses .= "<option value='" . $Sv->somar_data($atual, 0, 9,  0, true) . "'>$MesExtenso - $AnoExtenso</option>";
    $a1 = explode("/", $Sv->somar_data($atual, 0, 10,  0, true)); $MesExtenso = MesExt($a1[1]); $AnoExtenso = $a1[2]; $optMeses .= "<option value='" . $Sv->somar_data($atual, 0, 10,  0, true) . "'>$MesExtenso - $AnoExtenso</option>";
    $a1 = explode("/", $Sv->somar_data($atual, 0, 11,  0, true)); $MesExtenso = MesExt($a1[1]); $AnoExtenso = $a1[2]; $optMeses .= "<option value='" . $Sv->somar_data($atual, 0, 11,  0, true) . "'>$MesExtenso - $AnoExtenso</option>";
    $a1 = explode("/", $Sv->somar_data($atual, 0, 12,  0, true)); $MesExtenso = MesExt($a1[1]); $AnoExtenso = $a1[2]; $optMeses .= "<option value='" . $Sv->somar_data($atual, 0, 12,  0, true) . "'>$MesExtenso - $AnoExtenso</option>";

    $retorno["meses"] = $optMeses;
}
  • You could put the html of your select?

  • <select class="input-medium tuning" name="sel_account" id="sel_account"></select>, but is fed via jquery. But it is feeding right.

  • $("#sel_mes option:selected").val(); works as expected.

3 answers

5


You can take the value of the selected item from the is using the function .val() on its own select.

See working:

$(function(){
    $('button').click(function(){
        alert($('#sel_mes').val());
    });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select class="input-medium ajuste" id="sel_mes">
  <option value="09/03/2014">Março - 2014</option>
  <option value="09/04/2014">Abril - 2014</option>
  <option value="09/05/2014">Maio - 2014</option>
  <option value="09/06/2014">Junho - 2014</option>
  <option value="09/12/2015">Dezembro - 2015</option>
  <option value="09/01/2016">Janeiro - 2016</option>
</select>

<button>Valor</button>

  • It must be another part of my code that is generating some error, because I have exactly like this. I collect a combo on the side and it works I will analyze again.

  • @Joelhenriquerother Edit your question and add that snippet of your full code.

  • @Joelhenriquerother Did you find the problem? If you can then leave a comment stating what was causing the error.

  • I didn’t think so, but as I’m feeding a variable in the return I’m using it and of course, with no time to see what could generate because the code is simple, strange this. In the end it must be a comma or error in the code we always find, I left for a second plan, thank you

  • I am new here, I do not know can exchange contacts. But if you can have skype?

3

Using simple javascript:

var ValorA = document.getElementById("sel_mes").value

0

.NET

alert($('#cod_unidademedida').val());

<div class="form-group">
    @Html.Label("Unidade de Medida", new { @class = "control-label col-md-4" })
    <div class="col-md-2">
        @Html.DropDownList("cod_unidademedida", null, "--Selecione--", htmlAttributes: new { @class = "form-control" })
        @Html.ValidationMessageFor(model => model.cod_unidademedida, "", new { @class = "text-danger" })
    </div>
</div>

Browser other questions tagged

You are not signed in. Login or sign up in order to post.