Picking text from a given column with jquery

0

Good afternoon.

I need to get the value of a DropDownList in a given column of a table.

This would be the table:

 @model IEnumerable<Santander.Web.MVC.ValidacaoGanhadores.Models.Ganhadores>


<table id="tbValidacao">
    <thead>
        <tr style="height: 35px">
            <td></td>
            <td>Manter</td>
            <td>Matricula</td>
            <td>Nome</td>
            <td>Status</td>
            <td>Validado</td>

        </tr>
    </thead>
    <tbody>
        @{
            var count = 1;

            foreach (var item in Model)
            {
                @*item.Matricula.Contains("999") ? "Nao" : "Sim"*@
                <tr style="height: 35px">
                    <td>@count</td>
                    <td>@Html.DropDownList("DropManter", listItems)</td>
                    <td>@Html.TextBox("txtMatricula", "", new { @readonly = "readonly" })</td>
                    <td>@Html.TextBox("txtNome", "", new { @readonly = "readonly" })</td>
                    <td>@Html.TextBox("txtStatus", "", new { @readonly = "readonly" })</td>
                    <td>@(item.Rede == "Rede SP Capital" ? 1 : 2)</td>
                </tr>
                count = count + 1;
            }

        }






    </tbody>
</table>

The number of rows is variable, I need to get the value of this DropDownList of each row as soon as it is changed.

I was trying the following way:

 $('#tbValidacao tbody tr td').change(function () {

        var conteudo = $('#DropManter option:selected').text();
        alert(conteudo);

    })

It always returns the value of the first line.

    
asked by anonymous 01.02.2016 / 18:11

3 answers

2

Paulo, to make sure your HTML is valid, I advise you to assign a unique ID to each element of your page, so I advise you to use this cont in your favor, but you can keep all DropDownList with even name

@Html.DropDownList("DropManter-" + cont, listItems, new { @Name = "DropManter" })

Then assign the direct change event to DropDownList (a.k.a select ) with name='DropManter' , you can access the current select by event.target and / or this :

$("select[name='DropManter']").change(function (event) {
    var dropManter = $(event.target);
    var conteudo = $('option:selected', dropManter).text();
    alert(conteudo);
})
    
01.02.2016 / 18:59
1

The @TobyMosque posted option works, and will be your best option if you want to send those values somewhere. I'll leave only an alternate form using the $(this) that @TobyMosque commented on.

To do this, simply change the script to this:

            <script>
                $("select[name='DropManter']").change(function(){
                    var valor = $(this).val();
                    var texto = $('option:selected', $(this)).text();
                    alert(valor + ': ' + texto );
                });
            </script>

In this example it will be executed by manipulating any element (select) within the tbValidacao id. By changing the value it will select the value of the select being manipulated at the moment.

Take a look at dotNetFiddle .

    
01.02.2016 / 20:10
0
Hello, to get the "value" value of a select I usually use in jQuery .val () example:

$("#usuario").change(function () {
   alert($("#usuario").val());
});
    
01.02.2016 / 18:40