0

I'm trying to get each row in array. I get the value of all textbox but the problem is the dropdown list. It doesn't get the current selected index. Here is my code so far:

var table_data = [];    
        $('#table_assign tr').each(function () {    
            var row_data = [];    
            $('td', this).each(function () {    
                row_data.push($(this).text());    
            });    
            table_data.push(row_data);    
        });    
        alert(table_data);
    }

I want to get the current selected index in each row. How to do that? any ideas?


Update:

This is how I add the data into the table

 $.get('LoadserviceSplit', {"sectcode": 001},
    function (jsonResponse) {
        if (jsonResponse != null) {
            var table2 = $("#table_assign");
            $.each(jsonResponse, function (key, value) {
                                             var rowNew = $("<tr><td></td><td></td><td></td><td></td><td></td><td></td></tr>");
                rowNew.children().eq(0).text(value['serviceId']);
                rowNew.children().eq(1).text(value['title']);
                  rowNew.children().eq(2).html('<input type="text" class="selectko"/>');                        
                rowNew.children().eq(3).html('<select class="selectko" id="employee' + ctr+ '">'); // this is the dropdown list
                rowNew.children().eq(4).html('<select class="form-control input-sm">\n\
                                            <option value="Low">Normal</option>\n\
                                            <option value="Normal">Low</option>\n\
                                            <option value="High">High</option></select>');
                rowNew.children().eq(5).html('<input type="text" class="form-control input-sm"/>');

            });

          ctr++;

        }

    });

the id of the option box is employee with auto increment. sample "employee1",then next dropdown list id will be "employee2".

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
rob
  • 5
  • 2
  • 11

1 Answers1

0

To get the selected index try using the selected selector: http://api.jquery.com/selected-selector/

For example (this will also get your index):

$("select[name='mydropdown'] option:selected").index()

To just grab the element: $("select[name='mydropdown'] option:selected")

However, if possible you should use the ID selector (faster): $("#mydropdown option:selected")

kindagonzo
  • 1,468
  • 2
  • 10
  • 8