php - 使用动态创建的选择字段的 onchange 显示隐藏动态生成的文本框

标签 php javascript html

我想使用 MySQL、PHP、JS 和 HTML 动态生成如下表:

___________________________________________
[] |   Please Select Service  |  Text Box |
-------------------------------------------
    Add Row         Delete Row

所以第一列是一个用于删除行的复选框。 第二列是从数据库动态填充的选择框 第三列默认隐藏,选中“其他”时显示,不选中“其他”时隐藏。 因此,当按下“添加行”时,它将如下所示:

___________________________________________
[] |   Please Select Service  |  Text Box |
-------------------------------------------
___________________________________________
[] |   Please Select Service  |  Text Box |
-------------------------------------------
    Add Row         Delete Row

我的问题是我只能隐藏/显示第一个文本框,除非在按下“添加行”时第一个文本框已经显示,如果发生这种情况,新文本框将显示但不能隐藏, “添加行”和“删除行”按钮的功能与其名称状态相同。我猜它与选择字段中最后一个选项元素关联的值有关。

可以在下面看到 addRow、deleteRow 和隐藏/显示函数的代码片段,以及我用来显示它的 HTML。

JS

var boxCount = 1;
    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);

            var theSelect = document.getElementById('services');
            var lastValue = theSelect.options[theSelect.options.length - 1].value;

            newcell.innerHTML = table.rows[0].cells[i].innerHTML;
            switch(newcell.childNodes[0].type) {
                case "text":
                        newcell.childNodes[0].value = "";
                        newcell.childNodes[0].id = "otherService" + boxCount;
                        break;
                case "checkbox":
                        newcell.childNodes[0].checked = false;
                        break;
                case "select-one":
                        newcell.childNodes[0].selectedIndex = 0;
                        newcell.childNodes[0].options[lastValue].value = boxCount;
                        break;
            }
        }
    }
    function deleteRow(tableID) {
        try {
        var table = document.getElementById(tableID);
        var rowCount = table.rows.length;

        for(var i=0; i<rowCount; i++) {
            var row = table.rows[i];
            var chkbox = row.cells[0].childNodes[0];
            if(null != chkbox && true == chkbox.checked) {
                if(rowCount <= 1) {
                    alert("Cannot delete all the rows.");
                    break;
                }
                table.deleteRow(i);
                rowCount--;
                i--;
            }


        }
        }catch(e) {
            alert(e);
        }
    }

    function hideShowOtherBox(e) {
        var theSelect = document.getElementById('services');
        var lastValue = theSelect.options[theSelect.options.length - 1].value;
        var ele = 'otherService' + lastValue;

        if (e.options[e.selectedIndex].text == "Other") {
            if (document.getElementById(ele).style.display == "none") {
                document.getElementById(ele).style.display = "block";
            }
        } else {
            document.getElementById(ele).style.display = "none";
        }
    }

HTML

<table id="dataTable" width="350px" border="1">
    <tr>
        <td><input type="checkbox" name="chk[]"/></td>
        <td>
            <select id='services' name="services[]" onChange='hideShowOtherBox(this);'>
                <option value="Select">Please Select</option>
                <?php
                    $servicesList = getAllServices();
                    foreach($servicesList as $x) {
                        echo "<option value='$x'>$x</option>\n";
                    }
                ?>
                <option value='0'>Other</option>
            </select>
        </td>
        <td><input id='otherService0' style='display:none;' type="text" name="otherService[]"/></td>
    </tr>
</table>
<input type="button" value="Add Row" onclick="addRow('dataTable')" />

<input type="button" value="Delete Row" onclick="deleteRow('dataTable')" />

最佳答案

试试这个:

var boxCount = 1;

function addRow(tableID) {
    var table = document.getElementById(tableID);

    var rowCount = table.rows.length;
    var row = table.insertRow(rowCount);
}

关于php - 使用动态创建的选择字段的 onchange 显示隐藏动态生成的文本框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13813572/

相关文章:

html - 对齐两个 DIV

javascript - SVG 图形不会出现在使用 javascript 和内部样式的第二个 float 相对 div 列中

php - 验证 PHP 中的 float 值

php - 使用自定义 PHP MVC 在另一个 foreach 循环中进行 foreach 循环

php - 无法在 php 中使用 strtotime ("2015-02-14") 函数获得正确的结果

php - javascript 和 php 验证?

php - 谁能提供一些有关 Zend Framework 中表单单元测试的资源?

javascript - 停止计时器不工作:

javascript - jQuery:将数组项与文本匹配>从数组中删除项

php - 不通过_POST 发送数据的简单表单