HTML选择框,从servlet中选择数据 [英] HTML Select Box, selected data from servlet

查看:115
本文介绍了HTML选择框,从servlet中选择数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

美好的一天!

我在html中的选择框有问题。我在我的简单CRUD项目的编辑部分,用户可以编辑之前,选择的数据将首先显示,我通过servlet检索到它。



现在我希望我检索的数据是我选择框中的SELECTED(默认值)。 $ {product.category}

 < select size =1 name =category> 
< option value =1> Dogs< / option>
< option value =2>猫< / option>
< option value =5>其他< / option>
< / select>

我尝试像这样插入它,但它不起作用。

 < select size =1name =categoryselected = $ {product.category}> 
< option value =1> Dogs< / option>
< option value =2>猫< / option>
< option value =5>其他< / option>
< / select>

我想这样做。If($ {product.category} == 1) ,selected = option 1 ...



我曾见过类似于这个在其中一个论坛中,但它是PHP格式。我怎样才能使用JSP?



非常感谢。

解决方案

选定的属性必须位于HTML < option> 元素上,并且它应该仅 在选项值匹配时设置。最优雅的方式是使用条件运算符?:

 < ;选择name =category> 
< option value =1$ {product.category =='1'? 'selected':''}> Dogs< / option>
< option value =2$ {product.category =='2'? 'selected':''}> Cats< / option>
< option value =5$ {product.category =='5'? 'selected':''}>其他< / option>
< / select>

如果您有一些 List Map 。例如。 a List< Category> 其中 Category 具有 id code> name properties。

 < select name =category> 
< c:forEach items =$ {categories}var =category>
< option value =$ {category.id}$ {product.category == category.id? 'selected':''}> $ {category.name}< / option>
< / c:forEach>
< / select>

这样您就不需要为所有选项重复相同的操作。


Good day!

I am having a problem with the select box in html. I am on the EDIT portion of my simple CRUD project and before users can edit, the chosen data will be shown first and I retrieved it in the database through the servlet.

Now I want the data I retrieve be the one SELECTED (default) in my select box. ${product.category}

<select size="1" name="category">
        <option value ="1">Dogs</option>
        <option value ="2">Cats</option>
        <option value ="5">Others</option>
</select>

I tried inserting it like this but it doesn't work.

<select size="1" name="category" selected=${product.category}>
        <option value ="1">Dogs</option>
        <option value ="2">Cats</option>
        <option value ="5">Others</option>
</select>

I want to do something like this.. If (${product.category}==1), selected=option 1...

I've seen something like THIS in one of the forums but it is in PHP format. How can I do it using JSP?

Thank you very much.

解决方案

The selected attribute has to go on the HTML <option> element and it should only be set when the option value matches. Most elegant way is to use the conditional operator ?:.

<select name="category">
    <option value="1" ${product.category == '1' ? 'selected' : ''}>Dogs</option>
    <option value="2" ${product.category == '2' ? 'selected' : ''}>Cats</option>
    <option value="5" ${product.category == '5' ? 'selected' : ''}>Others</option>
</select>

Better would be if you have the items in some List or Map. E.g. a List<Category> where Category has id and name properties.

<select name="category">
    <c:forEach items="${categories}" var="category">
        <option value="${category.id}" ${product.category == category.id ? 'selected' : ''}>${category.name}</option>
    </c:forEach>
</select>

This way you don't need to repeat the same thing for all options.

这篇关于HTML选择框,从servlet中选择数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆