Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/269.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
JavaScript-如何在选择框(动态生成的选择框)中保留上次提交的值_Javascript_Php_Html - Fatal编程技术网

JavaScript-如何在选择框(动态生成的选择框)中保留上次提交的值

JavaScript-如何在选择框(动态生成的选择框)中保留上次提交的值,javascript,php,html,Javascript,Php,Html,为了修改下面的代码,你能帮我修改一下吗?提交表单后,我希望在第二个选择框(id=“detail”)中保留值。如您所见,第二个框是通过JavaScript动态生成的。 谢谢 <form method="get" action="index.php"> <script> function configureDropDownLists(aa,detail) { var colours = ['red',

为了修改下面的代码,你能帮我修改一下吗?提交表单后,我希望在第二个选择框(id=“detail”)中保留值。如您所见,第二个框是通过JavaScript动态生成的。 谢谢

    <form method="get" action="index.php">

    <script>
            function configureDropDownLists(aa,detail) {
                var colours = ['red', 'green'];
                var cars = ['audi', 'fiat'];


                switch (aa.value) {
                    case 'colours':
                        detail.options.length = 0;
                        for (i = 0; i < colours.length; i++) {
                            createOption(detail, colours[i], colours[i]);
                        }
                        break;
                    case 'cars':
                        detail.options.length = 0; 
                        for (i = 0; i < cars.length; i++) {
                            createOption(detail, cars[i], cars[i]);
                        }
                        break;

                    default:
                        detail.options.length = 0;
                        break;
                }
            }

            function createOption(object, text, value) {
                var opt = document.createElement('option');
                opt.value = value;
                opt.text = text;
                object.options.add(opt);
            }
        </script>


        <select id="object" name="objects" onchange="configureDropDownLists(this,document.getElementById('detail'))">
            <option value=""></option>
            <option <?php if ($_GET['objects'] == 'colours') { ?>selected="true" <?php }; ?>value="colours">colours</option>
            <option <?php if ($_GET['objects'] == 'cars') { ?>selected="true" <?php }; ?>value="cars">cars</option>
        </select>


        <select id="detail">
        </select>


        <input type="submit" value="Submit" class="submit" />

    </form>

功能配置下拉列表(aa,详细信息){
变量颜色=['红色','绿色'];
var汽车=[‘奥迪’、‘菲亚特’];
开关(aa.值){
“颜色”案例:
detail.options.length=0;
对于(i=0;i颜色
value=“cars”>汽车

由于您是动态生成选项的,因此在提交表单后,这些选项甚至不存在。因此,我认为应该在页面加载时调用该函数一次以创建它们

此外,您还必须为创建选项的函数指定默认值,该值应设置为选中值

以下是我建议的更改:

<form method="get" action="index.php">

    <script>
            function configureDropDownLists(aa,detail,selected) {
                //alert(selected);
                var colours = ['red', 'green'];
                var cars = ['audi', 'fiat'];


                switch (aa.value) {
                    case 'colours':
                        detail.options.length = 0;
                        for (i = 0; i < colours.length; i++) {
                            createOption(detail, colours[i], colours[i], selected);
                        }
                        break;
                    case 'cars':
                        detail.options.length = 0; 
                        for (i = 0; i < cars.length; i++) {
                            createOption(detail, cars[i], cars[i], selected);
                        }
                        break;

                    default:
                        detail.options.length = 0;
                        break;
                }
            }

            function createOption(object, text, value, selected) {
                var opt = document.createElement('option');
                opt.value = value;
                opt.text = text;
                opt.selected = (selected == value) ? "selected" : '';
                object.options.add(opt);
            }


        </script>


        <select id="object" name="objects" onchange="configureDropDownLists(this,document.getElementById('detail'),'<?php echo isset($_GET['detail']) ? $_GET['detail'] : ''; ?>')">
            <option value=""></option>
            <option <?php if ($_GET['objects'] == 'colours') { ?>selected="true" <?php }; ?>value="colours">colours</option>
            <option <?php if ($_GET['objects'] == 'cars') { ?>selected="true" <?php }; ?>value="cars">cars</option>
        </select>


        <select id="detail" name="detail">
        </select>


        <input type="submit" value="Submit" class="submit" />

    </form>
<script>
    configureDropDownLists(document.getElementById('object'),document.getElementById('detail'),'<?php echo isset($_GET['detail']) ? $_GET['detail'] : ''; ?>')
</script>

功能配置下拉列表(aa、详细信息、选定){
//警报(选定);
变量颜色=['红色','绿色'];
var汽车=[‘奥迪’、‘菲亚特’];
开关(aa.值){
“颜色”案例:
detail.options.length=0;
对于(i=0;i由于您是动态生成选项的,因此在提交表单后,这些选项甚至不存在。因此,我认为应该在页面加载时调用该函数一次以创建它们

此外,您还必须为创建选项的函数指定默认值,该值应设置为选中值

以下是我建议的更改:

<form method="get" action="index.php">

    <script>
            function configureDropDownLists(aa,detail,selected) {
                //alert(selected);
                var colours = ['red', 'green'];
                var cars = ['audi', 'fiat'];


                switch (aa.value) {
                    case 'colours':
                        detail.options.length = 0;
                        for (i = 0; i < colours.length; i++) {
                            createOption(detail, colours[i], colours[i], selected);
                        }
                        break;
                    case 'cars':
                        detail.options.length = 0; 
                        for (i = 0; i < cars.length; i++) {
                            createOption(detail, cars[i], cars[i], selected);
                        }
                        break;

                    default:
                        detail.options.length = 0;
                        break;
                }
            }

            function createOption(object, text, value, selected) {
                var opt = document.createElement('option');
                opt.value = value;
                opt.text = text;
                opt.selected = (selected == value) ? "selected" : '';
                object.options.add(opt);
            }


        </script>


        <select id="object" name="objects" onchange="configureDropDownLists(this,document.getElementById('detail'),'<?php echo isset($_GET['detail']) ? $_GET['detail'] : ''; ?>')">
            <option value=""></option>
            <option <?php if ($_GET['objects'] == 'colours') { ?>selected="true" <?php }; ?>value="colours">colours</option>
            <option <?php if ($_GET['objects'] == 'cars') { ?>selected="true" <?php }; ?>value="cars">cars</option>
        </select>


        <select id="detail" name="detail">
        </select>


        <input type="submit" value="Submit" class="submit" />

    </form>
<script>
    configureDropDownLists(document.getElementById('object'),document.getElementById('detail'),'<?php echo isset($_GET['detail']) ? $_GET['detail'] : ''; ?>')
</script>

功能配置下拉列表(aa、详细信息、选定){
//警报(选定);
变量颜色=['红色','绿色'];
var汽车=[‘奥迪’、‘菲亚特’];
开关(aa.值){
“颜色”案例:
detail.options.length=0;
对于(i=0;i很漂亮,谢谢你,安德鲁。。它显示了我需要学习多少才能正确理解它美丽的,谢谢你安德鲁。。它显示了我需要学习多少才能正确理解它。。