Get the value of select

3

My Select looks 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, ie the date 09/04/2014 and not the text Abril - 2014

I'm using this:

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

I have tried this too

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

But both return Abril - 2014

Colote via JQuery So

        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);
           }                               
        });            
    }

NO 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;
}
    
asked by anonymous 09.03.2015 / 21:39

3 answers

5

You can get the value of the selected item in is using the function .val() in the select itself.

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><selectclass="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>
    
09.03.2015 / 21:48
3

Using simple javascript:

var ValorA = document.getElementById("sel_mes").value
    
09.03.2015 / 21:49
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>
    
12.07.2017 / 14:19