使用请求参数在下拉列表中选择一个选项 [英] selecting an option in a dropdown list using request parameters

查看:266
本文介绍了使用请求参数在下拉列表中选择一个选项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对这件事很陌生,所以请耐心等待。
我需要从下拉列表中选择需要设置当前选定值的数据库中的值。该数据将作为请求参数传递给前端。我尝试使用selected =parameter属性与选择标签,但似乎没有工作。我也尝试过value =,没有结果。
任何帮助表示赞赏。谢谢

I'm kind of new to this so please bear with me. I have a value from the db which needs to set the currently selected value in a dropdown .this is passed to the front end as a request parameter. I tried using selected="parameter" attribute with the select tag but doesnt seem to work. I also tried value="", no result. Any help is appreciated. Thanks

推荐答案

如果您想在< select> ,将选定的属性放入相应的选项中。

If you want to serve the jsp with a selected value in the <select>, put the selected attribute in the corresponding option.

<select id="dropdown">
  <option value="1">option 1</option>
  <option value="2" selected>option 2</option>
  <option value="3">option 3</option>
</select>

使用JSTL标记< c:if> 来测试请求参数和当前值之间的相等性。例如,使用名为 selectValue 的请求参数:

Use the JSTL tag <c:if> to test for equality between the request parameter and the current value. For instance, with a request parameter named selectValue:

<option 
    <c:if test='${param.selectValue == currentOption}'> selected </c:if>
>
</option

查看以下问题以获取详细信息:使用JSTL在选择标记中填充选定项目?

Take a look at the following question for details: Selected item populating in Select tag using JSTL?

详细说明此问题(< c:forEach> ),当循环选项时,输出选中< option> 的值等于请求参数时,在< option> (code untested):

To elaborate on the matter, in case you're building your options dinamically (<c:forEach>), while looping the options, output selected in the <option> when the current <option>'s value is equal to the request parameter (code untested):

<select id="dropdown">
    <c:forEach var="item" items="${list}">
        <option value="<c:out value='${item}' />"
            <c:if test="${param.selectValue == item})"> selected </c:if>  >
            <c:out value="${item}" />
        </option>
    </c:forEach>
</select>

如果选项是静态的,您可以在每个 < option> (code untested):

In case the options are static, you could add something like this in each <option> (code untested):

  <option value="1" 
    <c:if test="${param.selectValue == 1})"> selected </c:if> >
    option 1 
  </option>
  <option value="2" 
    <c:if test="${param.selectValue == 2})"> selected </c:if> >
    option 2
  </option>

如果您想通过javascript设置所选值,请使用

If you want to set the selected value through javascript, use for instance

document.getElementById("dropdown").value = <c:out value='${param.selectValue}'/>

这篇关于使用请求参数在下拉列表中选择一个选项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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