如何在JSP页面中的选项标签上使用onClick()或onSelect()? [英] How to use onClick() or onSelect() on option tag in a JSP page?
问题描述
如何使用 onClick()
或 onSelect()
带选项
标签?下面是我尝试实现的代码,但它没有按预期工作。
How to use onClick()
or onSelect()
with option
tag? Below is my code in which I tried to implement that, but it is not working as expected.
注意:其中 listCustomer
在JSP页面中获取域对象列表。
Note: where listCustomer
domain object list getting in JSP page.
<td align="right">
<select name="singleSelect" ">
<c:forEach var="Customer" items="${listCustomer}" >
<option value="" onClick="javascript:onSelect(this);> <c:out value="${Customer}" /></option>
</c:forEach>
</select>
</td>
如何修改它以检测是否选择了某个选项?
How do I modify it to detect that an option is selected?
推荐答案
onSelect()
或
事件。前者指的是选择文本(即通过单击+拖动文本字段),因此只能与< option>
标记支持onClick()< text>
和<< textarea>
标签。 onClick()
事件可与< select>
标记一起使用 - 但是,您可能正在寻找功能最好使用 onChange()
事件,而不是 onClick()
。
Neither the onSelect()
nor onClick()
events are supported by the <option>
tag. The former refers to selecting text (i.e. by clicking + dragging across a text field) so can only be used with the <text>
and <textarea>
tags. The onClick()
event can be used with <select>
tags - however, you probably are looking for functionality where it would be best to use the onChange()
event, not onClick()
.
此外,看看你的< c:...>
标签,您还尝试使用 JSP 语法。那只是......不正确。
Furthermore, by the look of your <c:...>
tags, you are also trying to use JSP syntax in a plain HTML document. That's just... incorrect.
回应你对这个答案的评论 - 我几乎听不懂。但是,听起来您想要做的就是获取用户刚刚选择的< option>
标记的值。在这种情况下,你希望有类似的东西:
In response to your comment to this answer - I can barely understand it. However, it sounds like what you want to do is get the value of the <option>
tag that the user has just selected whenever they select one. In that case, you want to have something like:
<html>
<head>
<script type="text/javascript">
function changeFunc() {
var selectBox = document.getElementById("selectBox");
var selectedValue = selectBox.options[selectBox.selectedIndex].value;
alert(selectedValue);
}
</script>
</head>
<body>
<select id="selectBox" onchange="changeFunc();">
<option value="1">Option #1</option>
<option value="2">Option #2</option>
</select>
</body>
</html>
这篇关于如何在JSP页面中的选项标签上使用onClick()或onSelect()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!