如何使用onClick()
或onSelect()
与option
标签?下面是我试图实现它的代码,但它没有按预期工作.
注意:listCustomer
在JSP页面中获取域对象列表.
<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>
Run Code Online (Sandbox Code Playgroud)
如何修改它以检测是否选择了某个选项?
Ste*_*hen 84
标签既不支持onSelect()
也不onClick()
支持事件<option>
.前者指的是选择文本(即通过单击+拖动文本字段),因此只能与<text>
和<textarea>
标签一起使用.该onClick()
事件可以与<select>
标签一起使用- 但是,您可能正在寻找最好使用该onChange()
事件的功能,而不是onClick()
.
此外,通过<c:...>
标记的外观,您还尝试在纯HTML文档中使用JSP语法.那只是......不正确.
回应你对这个答案的评论 - 我几乎无法理解.但是,听起来你想要做的就是获取<option>
用户刚刚选择的标签的值.在这种情况下,你想要有类似的东西:
<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>
Run Code Online (Sandbox Code Playgroud)
Han*_*yer 21
更简化:您可以直接传递value属性!
<html>
<head>
<script type="text/javascript">
function changeFunc($i) {
alert($i);
}
</script>
</head>
<body>
<select id="selectBox" onchange="changeFunc(value);">
<option value="1">Option #1</option>
<option value="2">Option #2</option>
</select>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
警报将返回1
或2
.
小智 5
上面给出的答案有效,但是令人困惑,因为您两次使用了两个名称,并且不必要的代码行。您正在执行不必要的过程。
这是一个好主意,当调试代码以获取笔和纸并绘制小方框表示内存空间(即存储的变量),然后绘制箭头以指示变量何时进入小方框以及何时出现时(如果得到)被覆盖或被复制等。
如果使用下面的代码执行此操作,则会看到
var selectBox = document.getElementById("selectBox");
Run Code Online (Sandbox Code Playgroud)
放在盒子里然后待在那儿,之后就什么也不用做。
和
var selectBox = document.getElementById("selectBox");
Run Code Online (Sandbox Code Playgroud)
当您为选项列表选择一个selectBox的select ID时,它很难调试并且令人困惑。----您要操作/查询/选择哪个selectBox是将消失的本地变量selectBox还是您已分配给select标签的selectBox id
您的代码将一直有效,直到您对其进行添加或修改为止,然后您可以轻松地松开轨道并将所有内容混在一起
<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>
Run Code Online (Sandbox Code Playgroud)
一种更有效的方法是:
<html>
<head>
<script type="text/javascript">
function changeFunc() {
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>
Run Code Online (Sandbox Code Playgroud)
使用与您正在处理的程序和任务相匹配的描述性名称是一个好主意,当前正在编写一个类似的程序来使用您的代码接受和处理邮政编码,并使用描述性名称对其进行修改,目的是使计算机语言接近自然语言。
<script type="text/javascript">
function Mapit(){
var actualPostcode=getPostcodes.options[getPostcodes.selectedIndex].value;
alert(actualPostcode);
// alert is for debugging only next we go on to process and do something
// in this developing program it will placing markers on a map
}
</script>
<select id="getPostcodes" onchange="Mapit();">
<option>London North Inner</option>
<option>N1</option>
<option>London North Outer</option>
<option>N2</option>
<option>N3</option>
<option>N4</option>
// a lot more options follow
// with text in options to divide into areas and nothing will happen
// if visitor clicks on the text function Mapit() will ignore
// all clicks on the divider text inserted into option boxes
</select>
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
372624 次 |
最近记录: |