JSP下拉列表-使用所选项目
我有一个下拉列表和一个带有几个文本框的表单。我想用下拉列表中所选项目的详细信息填充此表单。
我在 java MVC 应用程序(或想要的应用程序)中执行此操作,并且在我的 jsp 页面中有类似这样的内容:
<select name="item">
<c:forEach items="${persons}" var="selectedPerson">
<c:set var="person" value="${selectedPerson}" />
<option value="$selectedPerson.id">${selectedPerson.LastName}</option>
</c:forEach>
</select>
Persons 是 Person 类的列表。
我想知道是否可以直接使用变量“person”来填写表单,例如:
<textarea name="name" rows="1" cols="34" >
${selectedPerson.Name}
</textarea>
以便在更改 selectedPerson 时更新表单的其余部分?
我知道如何在 C# 中执行此操作,但我没有 Java 技术的经验。
是否有必要将表单提交给 servlet 来执行此操作,或者可以在客户端上完成,因为从填充下拉列表的那一刻起,我的所有数据都在人员列表中?
I have a drop down list and a form with a few textboxes. I would like to populate this form with details of selected item in the drop down list.
I'm doing this in java MVC app (or wannabe) and I have in my jsp page something like this:
<select name="item">
<c:forEach items="${persons}" var="selectedPerson">
<c:set var="person" value="${selectedPerson}" />
<option value="$selectedPerson.id">${selectedPerson.LastName}</option>
</c:forEach>
</select>
Persons is a list of the Person class.
I wonder is it possible to use the variable 'person' directly to fill the form, for example:
<textarea name="name" rows="1" cols="34" >
${selectedPerson.Name}
</textarea>
so that the rest of the form updates when the selectedPerson is changed?
I know how to do this within c#, but I don't have experience with java technologies.
Is it necessary to submit the form to servlet to do this, or it can be done on the client, since I have all my data in the persons list, from the moment of populating the drop down list?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
${} 语法是 JSP 语法,只会在服务器上解析并运行一次以生成 HTML,然后作为 HTML 发送出去。然后,对选择列表的更改仅发生在客户端浏览器中:服务器对它们一无所知。
您想要做的是在选择框上注册一个 javascript 侦听器。您应该考虑使用理想的库来帮助您做到这一点。 JQuery 是一种非常流行的解决方案,如果您打算进行此类开发,那么值得一读。
如果您最终使用 JQuery,您将需要执行如下操作
一旦您阅读了基本的 JQuery 教程,这将更有意义,但基本上它的作用是每次选择列表更改值时,它都会获取选定的选项并将其内容设置为姓氏输入字段。希望这有帮助。
The ${} syntax is JSP syntax, which will only be parsed and run once on the server to generate the HTML, and then sent down the wire as HTML. The changes to the select list then just happen in the client browser: the server doesn't know anything about them.
What you want to do is register a javascript listener on the select box. You should look into using a library ideally to help you do this. JQuery is a very popular solution and is worth reading up on if you're going to be doing this type of development.
If you end up using JQuery, you'll want to do something like the following
This will make more sense once you've read a basic JQuery tutorial, but basically what it does is that each time the select list changes value, it gets the selected option and sets it's content to the lastName input field. Hope this helps.