javascript - 多个链式 SELECT 标签 JavaScript 和 jQuery

标签 javascript php jquery html select

美好的一天! :)

我使用 php 和 jQuery 创建了一个链式选择,如本页 http://www.yourinspirationweb.com/en/how-to-create-chained-select-with-php-and-jquery/ 所示。我创建了一个 JavaScript 来添加和删除选择标签我在互联网上的某个地方找到了它 但发生的情况是,当我在第一行选择一个类别时,子类别以及产品描述上的所有内容都将启用。就像这样/image/H9nfb.jpg

这是我的代码:

<html>

    <head>
        <script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
        <script type="text/javascript">
            $(document).ready(function() {
                $("select#type").attr("disabled", "disabled");


                $("select#category").change(function() {
                    $("select#type").attr("disabled", "disabled");
                    $("select#type").html("<option>wait...</option>");
                    var id = $("select#category option:selected").attr('value');
                    $.post("select_type.php", {
                        id: id
                    }, function(data) {
                        $("select#type").removeAttr("disabled");
                        $("select#type").html(data);
                    });

                });

                $("select#desc").attr("disabled", "disabled");
                $("select#type").change(function() {
                    $("select#desc").attr("disabled", "disabled");
                    $("select#desc").html("<option>wait...</option>");
                    var id = $("select#type option:selected").attr('value');
                    $.post("select_desc.php", {
                        id: id
                    }, function(data) {
                        $("select#desc").removeAttr("disabled");
                        $("select#desc").html(data);
                    });

                });
                $("form#select_form").submit(function() {
                    var cat = $("select#category option:selected").attr('value');
                    var type = $("select#type option:selected").attr('value');
                    var descr = $("select#desc option:selected").attr('value');
                    if (cat > 0 && type > 0 && descr > 0) {
                        var result = $("select#desc option:selected").html();
                        $("#result").html('your choice: ' + result);
                    } else {
                        $("#result").html("you must choose two options!");
                    }
                    return false;
                });
            });
        </script>
        <script type="text/javascript">
            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[1].cells[i].innerHTML;
                    //alert(newcell.childNodes);
                    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;
                    }
                }
            }

            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 <= 2) {
                                alert("Cannot delete all the rows.");
                                break;
                            }
                            table.deleteRow(i);
                            rowCount--;
                            i--;
                        }


                    }
                } catch (e) {
                    alert(e);
                }
            }
        </script>
    </head>

    <body>
        <?php include "select.class.php"; ?>
        <form id="select_form">
            <input type="button" value="Add" onclick="addRow('tableID')" />
            <input type="button" value="Delete" onclick="deleteRow('tableID')" />
            <table id="tableID">
                <tr>
                    <td></td>
                    <td>Category</td>
                    <td>SubCategory</td>
                    <td>Product Description</td>
                </tr>
                <tr>
                    <td>
                        <input type="checkbox" name="chkbox[]" />
                    </td>
                    <td>
                        <select id="category" name="category[]">
                            <?php echo $opt->ShowCategory(); ?></select>
                    </td>
                    <td>
                        <select id="type" name="type[]">
                            <option value="0">choose...</option>
                        </select>
                    </td>
                    <td>
                        <select id="desc" name="desc[]">
                            <option value="0">choose...</option>
                        </select>
                    </td>
                </tr>
            </table>
            <input type="submit" value="confirm" />
        </form>
        <div id="result" name="result[]"></div>
    </body>

</html>

希望有人能帮助我。 :)

最佳答案

<select type='text' class='chosen-select'>                                                                              
    <option value='initial'>Select One...</option>                                                                           
</select>

<select 'type='text' class='chosen-select' >                                                                               
    <option value='1' class='initial'>-----</option>                                                                                                                                             
</select>

使用类似的东西。如果用户选择“Select One”,则下一按钮中将显示“----”。相应地设置类(class)以使其适合您。

关于javascript - 多个链式 SELECT 标签 JavaScript 和 jQuery,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19207770/

相关文章:

php - 将任何音频文件转换为 ogg,而无需在 php 中使用 ffmpeg

jquery - 单选按钮组上 jQuery 验证的自定义错误消息

JQuery 重新加载按钮并更改值

javascript - 两种方式隔离绑定(bind) Angular Directive(指令) es6

javascript - Google reCAPTCHA float 通知未完全显示

javascript - 有没有一种可靠的方法可以让 Cypress 在测试失败后立即退出?

javascript - 更新日期或添加日期后日期选择器图标消失

javascript - Javascript 循环内的闭包

php - 如何使用 PHP 更改文件的扩展名?

javascript - jquery animate 让其他元素跳舞