【问题标题】:Keep the dropdown values after submit the form提交表单后保留下拉值
【发布时间】:2016-09-24 20:26:46
【问题描述】:

动态创建下拉框并通过 javascript 数组添加选项,我想在提交表单后保留这些值。假设我选择“OOR”和“2”,然后在提交表单后,我想在这些下拉列表中查看这些值。

谢谢。

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.2/jquery.min.js"></script>

<script language="javascript">

    OORs=new Array("1","2","3","4");
    NoOORs=new Array("A","B","C");

    populateSelect();
    $(function() {


        $('#fenv').change(function(){
            populateSelect();
        });

    });
    function populateSelect(){

        fenv=$('#fenv').val();

        $('#market').html('');
        if(fenv=='OOR'){
            $.each(OORs,function(index,t) {
                $("#market").append("<option value='"+t+"'>" +t+ "</option>");
            });
        }
        else {
            $.each(NoOORs,function(index,t) {
                $("#market").append("<option value='"+t+"'>" +t+ "</option>");
            });

        }

    }
</script>

<form>
    <select id="fenv" NAME="fenv">
        <option value="OOR2">OOR2</option>
        <option value="OOR">OOR</option>

    </select>

    <select id="market" name="market"></select>
    <input type="submit" name="submit" value="submit"  >
</form>

【问题讨论】:

  • 它不起作用怎么办?哪个版本的IE?你期望它做什么?它实际上在做什么?
  • 当我选择“OOR”时,市场下拉菜单应显示值 (1,2,3,4,5)。但是当我从第一个下拉列表(fenv)中选择 OOR 时,这里的市场下拉列表不会在 IE 浏览器中填充值。但在 Firefox 中,它显示的值很好。我用的是IE11版本。
  • 我整理了一个似乎在两种浏览器中都可以使用的 jsfiddle。 jsfiddle.net/y16se3uj
  • 但是在提交表单之后,如何保留我们在提交之前选择的值。
  • 查看上面的更新代码。非常感谢您的帮助。

标签: javascript jquery html internet-explorer coldfusion


【解决方案1】:

这很容易做到。

提交表单后(仅限同一页面),您可以在 CF 中检查提交条件并运行接受提交值的 JavaScript 函数。

  1. 提交表格
  2. fn populateSelect() 填充选择框
  3. CFIF 检查页面加载是否为表单提交
  4. 在FormSubmitSetSelectedValues(fenv, market) 值之后运行fn

    <form method="post">
        <select id="fenv" NAME="fenv">
            <option value="OOR2">OOR2</option>
            <option value="OOR">OOR</option>
        </select>
        <select id="market" name="market"></select>
        <input type="submit" name="submit" value="submit">
    </form>
    
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.2/jquery.min.js"></script>
    
    <script language="javascript">
        var OORs = ["1","2","3","4"], //declaring the OORs
            NoOORs = ["A","B","C"], //the NoOORs
            fenvRef = $('#fenv'),  //creating the ref using jQuery Once, so we do not need to do a DOM query again and again
            marketRef = $('#market'), // same for market
            populateSelect = function () {
    
                var fenv = fenvRef.val(), 
                    marketvalues = [];
    
                marketRef.html('');
    
                if ('OOR' === fenv) {
                    $.each(OORs, function(index,t) {
                        marketRef.append("<option value='" + t + "'>" + t + "</option>");
                        marketvalues.push(t);
                    });
                } else {
                    $.each(NoOORs, function(index,t) {
                        marketRef.append("<option value='" + t + "'>" + t + "</option>");
                        marketvalues.push(t);
                    });
                }
            },
            afterFormSubmitSetSelectedValues = function (fenv, market) { // upon reload this Fn() will set the selected values
                fenvRef.val(fenv);
                if ('OOR' === fenv) {
                    populateSelect();
                }
                marketRef.val(market);
            };
    
            $(function() {
                fenvRef.change(function() {
                    populateSelect();
                });
            });
    
            // this will populate the initial values
            populateSelect();
    
            <cfif isDefined('form') AND structKeyExists(form, 'submit')>
            //only executed if the form is previously submitted
                afterFormSubmitSetSelectedValues('<cfoutput>#form.fenv#</cfoutput>', '<cfoutput>#form.market#</cfoutput>');
            </cfif>
    </script>
    

祝你好运!

【讨论】:

    【解决方案2】:

    您可以在表单提交后使用隐藏字段来持久化数据。像这样:

        OORs=new Array("1","2","3","4");
        NoOORs=new Array("A","B","C");
    
        populateSelect();
        $(function() {
    
    
            $('#fenv').change(function(){
                populateSelect();
            });
    
        });
        function populateSelect(){
    
            fenv=$('#fenv').val();
            marketvalues = [];
    
            $('#market').html('');
            if(fenv=='OOR'){
                $.each(OORs,function(index,t) {
                    $("#market").append("<option value='"+t+"'>" +t+ "</option>");
                    marketvalues.push(t);
                });
            }
            else {
                $.each(NoOORs,function(index,t) {
                    $("#market").append("<option value='"+t+"'>" +t+ "</option>");
                    marketvalues.push(t);
                });
    
            }
            $("#marketvalues").val(marketvalues.join(","));
        }
    </script>
    
    <form method="post">
        <select id="fenv" NAME="fenv">
            <option value="OOR2" <cfif structKeyExists(form, "fenv") and form.fenv EQ "OOR2"> selected="selected"</cfif>>OOR2</option>
            <option value="OOR" <cfif structKeyExists(form, "fenv") and form.fenv EQ "OOR"> selected="selected"</cfif>>OOR</option>
    
        </select>
    
        <select id="market" name="market">
        <cfif structKeyExists(form, "marketvalues") and trim(form.marketvalues) NEQ "">
            <cfloop list="#form.marketvalues#" index="mv">
                <option value="#mv#" <cfif form.market EQ mv> selected="selected"</cfif>>#mv#</option>
            </cfloop>
        </cfif>
        </select>
        <input type="submit" name="submit" value="submit"/>
        <input type="hidden" name="marketvalues" id="marketvalues" value=""/>
    </form>
    

    【讨论】:

    • 您好 Pankaj,感谢您的帮助。像冠军一样工作。
    【解决方案3】:

    要保留一些数据,您需要使用 php session 或 post。

    第一次选择应该很容易:

    <select id="fenv" NAME="fenv">
        <option value="OOR2" <?php if($_POST["fenv"]=="OOR2") echo "selected";?>>OOR2</option>
        <option value="OOR" <?php if($_POST["fenv"]=="OOR") echo "selected";?>>OOR</option>
    </select>
    

    第二部分比较复杂。您可以做一些 javascript 魔术将其设置为 propper 值:

    var element = document.getElementById('market');
    element.value = "<?php echo(isset($_POST['market'])&&($_POST['market']!='')?$_POST['market']:'');?>";
    

    【讨论】:

    • 您好 LordNeo,感谢您的回复。但是我们使用的是coldfusion而不是php。你能告诉我我们如何在coldfusion中实现吗?
    猜你喜欢
    • 1970-01-01
    • 2016-07-04
    • 1970-01-01
    • 2023-03-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多