0

我正在使用以下代码从数据库中获取用户表值

 <%
String userid=(String)session.getAttribute("userid");
Connection con=Singleton.getMySqlConnection();
Statement st=con.createStatement();
ResultSet rs=st.executeQuery("select * from user where mail='"+userid+"'");
%>

并在jsp页面中显示值,如

<%while(rs.next()){%>

     <div class="divRegTableColF"><label for="USER_FIRST_NAME">First name: </label></div>
     <%if((rs.getString(2))!=null){%>
     <div class="divRegTableColS"><span> <input class="inputRegBox" name="fname" id="USER_FIRST_NAME" size="60" maxlength="50" value="<%=rs.getString(2)%>"></span>
     </div>
     <%}
     else
     {%>
     <input class="inputRegBox" name="fname" id="USER_FIRST_NAME" size="60" maxlength="50" value=""></span><span class="astRegPad"><font color="#000000">*</font>
     <%}%>


<select class="inputRegBox" name="country" size=1 maxlength=30 value="<%=rs.getString(13)%>">   
    <option>select</option>
    <option >Afghanistan</option>
    <option >Aland Islands</option>
    <option>Albania</option>
    <option>Algeria</option>
    <option>American Samoa</option>
    <option>Andorra</option>
    <option>Angola</option>
    <option>Anguilla</option>
    <option>Antarctica</option>
    <option>Antigua and Barbuda</option>
    <option>Argentina</option>
    <option>Armenia</option>
    <option>Aruba</option>
    <option>Australia</option>
    <option>Austria</option>
    <option>Azerbaijan</option>
    <option>Bahamas</option>
    <option>Bahrain</option>
    <option>Bangladesh</option>
    <option>Barbados</option>
    <option>Belarus</option>
    <option>Belgium</option>
    <option>Belize</option>
    <option>Benin</option>
    <option>Bermuda</option>
    <option>Bhutan</option>
    <option>Bolivia</option>
    <option>Bonaire, Saint Eustatius and Saba</option>
    <option>Bosnia and Herzegovina</option>
    <option>Botswana</option>
    <option>Bouvet Island</option>
    <option>Brazil</option> 
</select>
</div></div>
   </div>
<%}%>


enter code here

我在选择框中获取国家/地区值。如何选择选项值并在选择框中显示该值。

4

1 回答 1

4

在选项选择标记中添加条件,因为我不是 jsp 开发人员,但是您很容易将所有选项值添加到数组中并执行,

<select class="inputRegBox" name="country" size=1 maxlength=30 >   
    <option value="Afghanistan"  <%if((rs.getString(13))=='Afghanistan'){%> selected <%}%> >Afghanistan</option>
</select>
于 2013-10-31T08:51:33.743 回答