关于struts2标签select实现省市地级关联

在修改用户信息时: 

在头文件里引入 <script type="text/javascript" src="../js/city/address.js"></script>

<table>
     <tr>
      <td>用户名</td><td align="left"><s:property value="user.userName"></s:property></td>
     </tr>
     <tr>
      <td>邮箱</td><td><s:textfield name="email"></s:textfield></td>
     </tr>
     <tr>
      <td>联系电话</td><td><s:textfield name="telphone"></s:textfield></td>
     </tr>
     <tr>
      <td>省&份</td>
      <td style=" 400px ">
      <s:select list="#{}" name="province" id="province" headerKey="" headerValue="省/自治区/直辖市" cssStyle="100px;" theme="simple"></s:select>
      <s:select list="#{}" name="city" id="city" headerKey="" headerValue="市/地区/旗" cssStyle="100px;" theme="simple"></s:select>
      <s:select list="#{}" name="county" id="county" headerKey="" headerValue="区/市/县" cssStyle="100px;" theme="simple"></s:select>
    </td>
     </tr>
     <tr>
      <td>地址</td><td><s:textfield name="address"></s:textfield></td>
     </tr>s
    </table>

即可将全国省市等地区导入,实现省、市、县的下拉列表的显示。

针对上述方式在实践应用中出现的问题:特作修改(在按地区查询之后,返回,此时的省市县数据未必还在下拉列表中,根据项目要求准确性,修改如下:)

<script type="text/javascript">$(document).ready(function() {
   selectAddress();
  });
    function selectAddress()
       {
           setTimeout(function() {
              $("#province").val($("#selprovince").val());  
              initCitys($("#province"),$("#city"),$("#country")); 
              setTimeout(function() {              
                   $("#city").val($("#selcity").val());
                   initCountry($("#province"),$("#city"),$("#country"));
                   setTimeout(function() {                  
                      $("#country").val($("#selcountry").val());  
                    }, 1);  
              }, 1);
           }, 1);
       }

</script>

在script,js中添入此段代码,获取列表值,而返回后同样的页面,同样能获得值。

而在struts代码中修改如下:

<s:hidden id="selprovince" value="%{fc.province}" />
            <s:hidden id="selcity" value="%{fc.city}" />
            <s:hidden id="selcountry" value="%{fc.county}" />
         <select name="fc.province" id="province" style="100px;" ></select>   
         <select name="fc.city" id="city" style="100px;" ></select>        
         <select name="fc.county" id="country" style="100px;" ></select> 

效果很好,可以使用。在前几天的总结中只是为了效果,而且是第一次用,而若是在实际的使用中,还是后者更适用。

原文地址:https://www.cnblogs.com/yuedanfengqing/p/struts.html