页面刷新后我无法获取下拉值

时间:2016-01-05 09:32:19

标签: spring jsp

我有2个下拉菜单。 1.cusotmer 2.环境

onchange of customer dropdown我正在使用rest call和onchange方法获取环境名称。

我有一个GO按钮。点击go按钮后,我的页面必须刷新。基于选定的客户和环境。我得到了一些结果。但是那时我的环境下拉列表没有值。如果我改变我的客户然后加载它。

问题是页面刷新后我的下拉列表没有保留值。

这是我的代码..

<script>
    $(document).ready(function() {
        $("#customerDetails").change(function() {
            var value1 = $('#customerDetails :selected').text();
            $.ajax({
                type : 'POST',
                url : 'environments',
                data : {
                    selectedcustomername : value1
                },
                success : function(result){
                    getEnvNames(result);
                }

            });
        });
    });


function getEnvNames(result){
    $('#environmentName').empty().append('<option selected="selected" disabled="disabled">Select An Environment</option>');
    var data = JSON.parse(result);
    $.each(data, function(key, value)
    {
        $("#environmentName").append("<option>" + value.environments_name +" - "+ value.environments_purpose + "</option>");

    });

} 

 <b>Environment:</b>
                            <select  class="body"  name="environmentName" id="environmentName">                                                                                                                                                 
                            <option selected="selected" disabled="disabled">Select An Environment</option>                                                                                                                                                      
                            <select> </span>
                         </td>

这是我的控制器。

@RequestMapping(value = "/environments", method = RequestMethod.POST)
      public @ResponseBody String getEnvironmentNames(HttpServletRequest request,
                HttpServletResponse response,@RequestParam String selectedcustomername) throws SQLException {

        request.setAttribute("selectedcustomername", selectedcustomername);
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("environments", new Environments());
         List<Environments>  environmentnamesList= loginDelegate.getEnvironments(selectedcustomername);
         Collections.sort(environmentnamesList, new CustomComparator());
         Gson gson = new Gson();
         System.out.println("gson"+gson);
         String jsonString = gson.toJson(environmentnamesList);
         System.out.println("jsonString"+jsonString);
         return jsonString;
    }

这是我的按钮

<form:form method="get" action="retrieve" modelAttribute="customer" commandName="customer">

in between customer and environment dropdown ui..finally
 <input name="Submit" value="Go" type="submit" class="boldFontSize3" /> 

2 个答案:

答案 0 :(得分:0)

<强>解决方法1: 不要重新加载页面,而是使用ajax填充数据并在ui上显示

<强>溶液2: 使用javascript cookies存储下拉列表的当前状态并读取cookie并在页面加载时设置下拉列表。

使用cookies:

HTML:

<select id='select1'><option value='set1'>set1</option></select>

<select id='select2'><option value='set2'>set2</option></select>

脚本:

$(document).ready(function(){

$('#select2').change(function(){

var select2=$(this).val()
var select1=$('#select2').val()

$.cookie("select1", select1)
$.cookie("select2", select2)

})

//page load

var select1=$.cookie("select1")|| "default"
var select2=$.cookie("select2")|| "default"

$('#select1').val(select1)
$('#select2').val(select2)

})

答案 1 :(得分:0)

简单的方法是在控制器检索操作中读取所选客户值,并将相关环境值设置为模型。这样您也可以在页面刷新后获得下拉值。

相关问题