简体   繁体   English

动态填充表格每一行的下拉列表

[英]Dynamically filling drop downs on each row of table

I have a table with n number of rows depending on results.我有一个包含 n 行的表格,具体取决于结果。 There is a drop down on the top of the page.页面顶部有一个下拉菜单。 Depending on the selection of that drop down I want to dynamically fill a column on each row with a drop down of its own, based off of an xhttp.response.根据该下拉列表的选择,我想根据 xhttp.response 使用自己的下拉列表动态填充每行上的一列。

This is what I have so far:这是我到目前为止:

function getJobs(taskID){

        var xhttp = new XMLHttpRequest();
        var url = "dynamicdd.php";
        var data = new FormData();
        data.append('taskID', taskID);
        xhttp.open('POST', url, true);
        xhttp.send(data);
        $('#cTable tr').each(function(){
            xhttp.onreadystatechange = function() {
                if (xhttp.readyState == 4 && xhttp.status == 200) {
                document.getElementById("testSelect").innerHTML = xhttp.responseText;
                    }
                }
            });
        }

I'm able to change the first row successfully, but subsequent rows don't.我能够成功更改第一行,但后续行不能。 I've tried $(this).find(select:eq(0)).html() in replace of the document.getElementById() but no luck.我试过$(this).find(select:eq(0)).html()代替document.getElementById()但没有运气。 I feel like I'm close but lack expertise in jquery/javascript.我觉得我很接近但缺乏 jquery/javascript 的专业知识。 Thanks for your help.谢谢你的帮助。

You are re-assinging onreadystatechange function for each table row which is wrong,您正在为错误的每个表行重新分配 onreadystatechange 函数,

First assing the function for what it must do, then send the request then wait for the response ready then seek the element you want and replace element html with response text as below,首先为它必须做的事情分配函数,然后发送请求,然后等待响应准备好,然后寻找你想要的元素并用响应文本替换元素 html,如下所示,

function getJobs(taskID){

    var xhttp = new XMLHttpRequest();
    var url = "dynamicdd.php";
    var data = new FormData();
        data.append('taskID', taskID);
    xhttp.open('POST', url, true);
    xhttp.onreadystatechange = function() {
        if (xhttp.readyState == 4 && xhttp.status == 200) {
            $('#cTable tr').each(function(index, element){
                $(element).find('#testSelect').html(xhttp.responseText);     
            }
        }
    });
    xhttp.send(data);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM