如何在HTML下拉列表中包装文本[重复]

问题描述 投票:2回答:1

可能重复: Word wrap options in a select list

是否可以在html <option>框中包装长文本?

在我的JSP中,我有一段代码从循环中的数据库中提取值,并将它们附加到<option>列表的<select>部分。

以下是我引用的代码部分:

<select name='codes' onchange="showState(this.value)">  
                <option value="none">Select a code</option>  
                <%
                    //Pulls the ids and descriptions from the codes table and stores them in the first drop down
                    try
                    {
                        Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();  
                        Connection con = DriverManager.getConnection("url","username","password");  
                        Statement stmt = con.createStatement();  
                        ResultSet rs = stmt.executeQuery("select id, descr from ref_codes");

                        while(rs.next())
                        {
                            %>
                                <option value="<%=rs.getString(1)%>"><%=rs.getString(1)%> <%=rs.getString(2)%></option>  
                            <%
                        }

                        //Closes the database connection
                        stmt.close();
                        con.close();
                    }
                    catch (ClassNotFoundException e)
                    {
                        System.err.println("ClassNotFoundException: " + e.getMessage());
                    } 
                    catch (SQLException e)
                    {
                        System.err.println("SQLException: " + e.getMessage());
                    }
                    catch (Exception e)
                    {
                        System.err.println("Generic Exception: " + e.getMessage());
                    }       
                %>
                </select>

我的问题是,某些拉出的值太长,并且下拉框比页面的长度更长,因为它们都停留在同一条线上。每当我遇到一个非常长的字符串时,我想将它包装到下一行,但仍然包含在相同的<option>值中。

我环顾了谷歌,得到了不同的答案。有人说可以做到,有人说不可以。

然而,大多数答案似乎已经过时(从2000年代中期左右开始)和我尝试的那些,主要涉及使用<div>标签,我没有运气。这意味着选项仍然保留在一条线上,无论它们有多长,并且不会换行到下一行,尽管我指定了width标签的<div>

那么有人在之前用html做过这个或知道它是否可能?

html jsp drop-down-menu option
1个回答
2
投票

你不能只拉下你想要的下拉字符的最大数量,然后重新选择相同的字段,但从下一个字符开始,抓取相同的字符数等,然后使用HTML / CSS格式结合并使它们看起来像你想要的。

© www.soinside.com 2019 - 2024. All rights reserved.