在更改下拉列表时如何获取动态表行

时间:2013-12-02 08:58:05

标签: jquery

    $.ajax({
    type: 'GET',
    url: "/HostingProject/SIPAdminCustServlet",
    data: {ActionType:"1",userID:userID },
    success:function(responseJson){

    var tbody = $("#Account");
    alert("JSonResponse: "+responseJson);
    $.each(responseJson, function(index, account) {    // Iterate over the JSON array.
    var value = ATYpeMap[account.AccountTypeID];
     $('<tr>').appendTo(tbody)                     // Create HTML <tr> element, set its     text content with currently iterated item and append it to the <table>.
    .append($('<td>').text(account.AccountNumber))        // Create HTML <td> element, set its text content with id of currently iterated account and append it to the <tr>.
     .append($('<td>').text(value))        // Create HTML <td> element, set its text content with name of currently iterated account and append it to the <tr>.
    .append($('<td>').text(account.AccountStatus))
    .append ($('<select id="Accountchange"+r+ onchange="dropDownOnChange(this)"><option value="Valid">Valid</option><option value="Invalid">Invalid</option></select>'))

                    });
                }
        });

};
    var selectedValue;
    function dropDownOnChange(e){
    selectedValue=e.options[e.selectedIndex].value;
    alert("selectedValue:" + selectedValue);
}

嗨,当我选择下拉列表时,我正试图获取表格行。这是我的代码。请帮帮我。的问候,红宝石

1 个答案:

答案 0 :(得分:0)

很抱歉,首先我错误地理解了这个问题。试试这个....

var currentRow= $(e).closest("tr");
var AccountNo= $("td:eq(0)",$(currentRow)).text();

//You can change the index eq(0) to eq(1) and so on to get the next cells. 
//Alternatively you can give an attribute to column while adding like 
//.append($('<td>').text(account.AccountNumber).attr("colType='AcNo'")) 
//And get Account No Like var AccountNo= $("td[colType='AcNo']",$(tr)).text();  
//Usign the alternative approch will make your code independent of index. which is good for //maintainability and readability