---
title: "下拉列表选择完查询后跳回默认值解决方法"
author: "Edie"
date: "2012-05-17T17:04:44Z"
tags: []
categories: ["Web前端"]
slug: "untitled"
draft: false
lastmod: "2012-05-17T17:04:44Z"
id: 1533912
url: "https://edie.iteye.com/blog/1533912"
---

在页面中写了一个下拉列表选择查询，代码如下:

```
 订单类型：
        <select id="orderType" name="orderType" style="width:100px;">
          <option value='H' >酒店 </option>
              <option value='T' > 旅游</option>
     <option value='P' >商品</option>
   </select>

```

 

默认刚开始访问页面时下拉列表中选择的是酒店，但当选择“旅游”后，点击查询，下拉列表中的值又变回“酒店”，解决方案是：

在页面上写一个javascript脚本：

```
<script type="text/javascript">
$(function(){ 
        var orderType = '${orderType}'; //此orderType是由程序返回的值
        var orderTypeSel = $('#orderType').find('option');
        if (orderType == 'H') {
            orderTypeSel.get(0).selected = true;//根据下拉列表选择，如果值为"H"，将其设置为“酒店”，以下同
        } else if (orderType == 'T') {
            orderTypeSel.get(1).selected = true;
        } else if (orderType == 'P') {
            orderTypeSel.get(2).selected = true;
        } else{}
       }      
    ); 
</script>

```

 

主要执行过程:

当在页面从下拉列表中选择条件时，表单将条件提交给程序去数据库中查询，然后还会将查询出来的结果放在ModelMap中，在页面上就可以用${orderType}调用到。再次到页面上时，就根据程序传过来的值去判断该显示什么条件。

大概就是这样。。。。。。