如何修改我的页面并将我选择的选项传递给 java beans
我正在使用 html 代码根据我单击的所选选项来显示我的页面。但现在我也想将我的选择传递给 java beans。
显示功能的4 个选项
<tr>
<td class="title">Category:</td>
<td class="field">
<select name="list" onchange="display(this,'truck','car');">
<option>Please select:</option>
<option value="invisible">Bike</option>
<option value="invisible">Quad</option>
<option value="car">Car</option>
<option value="truck">Truck</option>
</select>
</td>
</tr>
我正在使用
<script type="text/javascript">
function display(obj,id1,id2) {
txt = obj.options[obj.selectedIndex].value;
document.getElementById(id1).style.display = 'none';
document.getElementById(id2).style.display = 'none';
if ( txt.match(id1) ) {
document.getElementById(id1).style.display = 'block';
}
if ( txt.match(id2) ) {
document.getElementById(id2).style.display = 'block';
}
}
</script>
脚本正在修改我的页面,执行以下操作:
<tbody id="car" style="display: none;">
现在
<tbody id="truck" style="display: none;">
,我如何将所选选项传递给 java beans?
i am using html code to display my page depending on the selected option i click on. But now i want to pass my selected choice to java beans too.
my 4 options
<tr>
<td class="title">Category:</td>
<td class="field">
<select name="list" onchange="display(this,'truck','car');">
<option>Please select:</option>
<option value="invisible">Bike</option>
<option value="invisible">Quad</option>
<option value="car">Car</option>
<option value="truck">Truck</option>
</select>
</td>
</tr>
Script i am using
<script type="text/javascript">
function display(obj,id1,id2) {
txt = obj.options[obj.selectedIndex].value;
document.getElementById(id1).style.display = 'none';
document.getElementById(id2).style.display = 'none';
if ( txt.match(id1) ) {
document.getElementById(id1).style.display = 'block';
}
if ( txt.match(id2) ) {
document.getElementById(id2).style.display = 'block';
}
}
</script>
display function is modifying my page doing the following:
<tbody id="car" style="display: none;">
and
<tbody id="truck" style="display: none;">
Now, how do i pass my selected options to java beans?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用 JSF 组件而不是纯 HTML 元素。 JSF 组件允许您将值绑定到 bean 属性。表示 HTML
例如
,
如果您使用的是 JSF 2.x,那么您也可以只使用
而不是所有 JavaScript 样板文件。我建议花一些时间阅读一些不错的 JSF 教程,看起来您通过手动编写所有 HTML/JS 完全错过了 JSF 的要点。或者,如果您打算完全控制 HTML/JS,最好寻找基于操作的 MVC 框架(例如 Spring MVC),而不是基于组件的 MVC 框架(例如 JSF)。Use a JSF component instead of a plain HTML element. JSF components allows you to bind the value to a bean property. The JSF component representing a HTML
<select>
element is the<h:selectOneMenu>
wherein you can specify the options using<f:selectItem>
or<f:selectItems>
.E.g.
with
If you're using JSF 2.x, then you can also just use
<f:ajax>
instead of all that JavaScript boilerplate. I'd suggest to invest some time in going through some decent JSF tutorials, it look like you're totally missing the point of JSF by writing all that HTML/JS manually. Or if you intend to have full control over HTML/JS, better look for an action based MVC framework such as Spring MVC instead of a component based MVC framework such as JSF.使用 AJAX。这允许您将数据发送到服务器,而无需更改网页上的任何内容。
尝试像 jQuery 这样提供 AJAX 的框架 (docs)以及许多其他用于构建 HTML5 应用程序的很酷的东西。
Use AJAX. That allows you to send data to the server without changing anything on the web page.
Try a framework like jQuery which offers AJAX (docs) and a lot of other cool stuff to build HTML5 applications.