简体   繁体   English

带2个选择框的动态表

[英]Dynamic table with 2 select boxes

I'm trying to do something like this . 我试图做类似这样 A table with a button to dynamically add a row. 带有按钮的表,用于动态添加行。 That works fine but my problem is with the 2 select boxes. 效果很好,但我的问题是2个选择框。 I want to change the select box that is in the same row and not just the first one. 我想更改同一行中的选择框,而不仅仅是第一个。 How can I do that? 我怎样才能做到这一点?

<form method="post" action="process.php">
    <input type="button" value="Add Row" onclick="addRow('dataTable')">
    <table id="dataTable" width="350px" border="1">
        <tbody>
            <tr>
                <td>
                    <input type="checkbox" name="chk[]">
                </td>
                <td>
                    <input type="text" name="txt[]">
                </td>
                <td>
                    <select id="countries" onchange="giveSelection(this.value)" name="tipo[]">
                        <option selected="selected" value="10000">--Select a country--</option>
                        <option value="1">Spain</option>
                        <option value="2">Germany</option>
                        <option value="3">U.S.A</option>
                    </select>
                </td>
                <td>
                    <select id="cities" name="cities[]">
                        <option selected="selected" data-option="10000">--Select a city--</option>
                        <option value="1" data-option="1">Madrid</option>
                        <option value="2" data-option="1">Barcelona</option>
                        <option value="3" data-option="1">Valencia</option>
                        <option value="4" data-option="2">Munich</option>
                        <option value="5" data-option="2">Berlin</option>
                        <option value="6" data-option="3">New York</option>
                        <option value="7" data-option="3">Houston</option>
                    </select>
                </td>
            </tr>
        </tbody>
    </table>
    <input type="submit" value="Send">
</form>

And here is my JS code: 这是我的JS代码:

var countries = document.querySelector('#countries');
var cities = document.querySelector('#cities');
var cities2 = cities.querySelectorAll('option');


function giveSelection(selValue) {
    cities.innerHTML = '';
    for (var i = 0; i < cities2.length; i++) {
        if (cities2[i].dataset.option === selValue) {
            cities.appendChild(cities2[i]);
        }
    }
}



function addRow(tableID) {
    var table = document.getElementById(tableID);
    var rowCount = table.rows.length;
    var row = table.insertRow(rowCount);
    var colCount = table.rows[0].cells.length;
    for (var i = 0; i < colCount; i++) {
        var newcell = row.insertCell(i);
        newcell.innerHTML = table.rows[0].cells[i].innerHTML;
        switch (newcell.childNodes[0].type) {
            case "text":
                newcell.childNodes[0].value = "";
                break;
            case "checkbox":
                newcell.childNodes[0].checked = false;
                break;
            case "select-one":
                newcell.childNodes[0].selectedIndex = 0;
                break;
        }
    }
}

Here is the updated version of your JsFiddle . 这是您的JsFiddle的更新版本。 The code can be simplified more but for now I have just updated your code so that you can understand the modifications easily. 该代码可以进一步简化,但是现在我刚刚更新了您的代码,以便您可以轻松地理解修改。

Following are the changes: 以下是更改:

  1. Added Jquery Library refernce 添加了Jquery库引用
  2. Used jQuery on change event instead of inline event 在更改事件而不是内联事件上使用jQuery
  3. Used jQuery to find the Cities select list in the same row of changed country 使用jQuery在更改的国家/地区的同一行中查找城市选择列表
  4. Used Classes instead of Ids for Both selects as Id is always used for uniqueness. 对于“两个选择”,使用“类”代替“ Ids”,因为Id始终用于唯一性。

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

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