2016-02-23 37 views
0

我需要在jsp中预先填充用户的国家名称。如何在jsp中预先填充国家

到目前为止,我有这样的:

<input type="text" name="foreigncountry" id="foreigncountry" class="nice input-text" size="20" maxlength="20" 
    <% 
     String country = userInfo.getAddressInfo().getCountry(); 
     if (country.equalsIgnoreCase("CA")) {             
      out.print("value='Canada'"); 
     else if((country.equalsIgnoreCase("KZ"))){ 
      out.print("value='Kazakhstan'"); 
    } %> 
> 

问题是我有大约180个国家和我不认为这是好事,有那么多的“如果其他人”在我的代码语句。

通常我会用下拉列表中选择国家是这样的:

<select id="countries" name="countries"> 
    <option value="AF">Afghanistan</option> 
    <option value="AL" selected>Albania</option> 
    ... 
</select> 

但我不知道如何将它们绑定,使其工作。

底线我宁愿有国家名称下拉列表,并从我从scriptlet获得的值预先填充国家。

任何意见非常感谢。

回答

0

好吧,最后我解决了它。不知道它的最佳解决方案,所以如果你知道更好的方式,请让我知道。

第一步:让输入类型为隐藏和JSP

<input type="hidden" name="foreigncountry" id="foreigncountry" class="nice input-text" size="20" maxlength="20" 
<% 
    String country = userInfo.getAddressInfo().getCountry(); 
    if (country != null && country.trim().length()>0 && !("US").equalsIgnoreCase(country)) {        
     out.print("value='" + country.trim() + "'"); 
    } else { 
     out.print("value=''"); 
    } 
%> 

得到值Step2:使用jQuery预填充国家

<select id="countries" name="countries"> 
    <option value="AF">Afghanistan</option> 
    <option value="AL">Albania</option> 
    ... 
</select> 

而且

<script> 
    var val = document.getElementById("foreigncountry").value; 
    $('#countries').val(val); 
</script> 
相关问题