admin管理员组文章数量:1320655
I have a jsp:
<form:select path="operation" onchange="myFunc(${oper.id})">
<c:forEach items="${operList}" var="oper">
<form:option label = "${oper.name}" value="${oper.id}|${oper.name}"/>...
And I have a js function myFunc(id) { alert(id); }
Then I change select value I see alert: undefined.
Wat's wrong?
I have a jsp:
<form:select path="operation" onchange="myFunc(${oper.id})">
<c:forEach items="${operList}" var="oper">
<form:option label = "${oper.name}" value="${oper.id}|${oper.name}"/>...
And I have a js function myFunc(id) { alert(id); }
Then I change select value I see alert: undefined.
Wat's wrong?
1 Answer
Reset to default 5You cant get the value ${oper.id}
outside the <forEach>
loop , as it is a element in the list.
To get the selected value from the dropdown onchange()
event , add the id
attribute to the field and get it in the javascript
jsp :
<form:select id="mySelect" path="operation" onchange="myFunc()">
<c:forEach items="${operList}" var="oper">
<form:option label = "${oper.name}" value="${oper.id}|${oper.name}"/>...
jquery:
function myFunc() {
var selectedValue= $("#mySelect").val();
alert(selectedValue);
}
And if you prefer to do it without jquery , here is the javascript version ,
function myFunc() {
var selectBox = document.getElementById("mySelect");
var selectedValue = selectBox.options[selectBox.selectedIndex].value;
alert(selectedValue);
}
or simply,
<form:select id="mySelect" path="operation" onchange="myFunc(value)">
<c:forEach items="${operList}" var="oper">
<form:option label = "${oper.name}" value="${oper.id}|${oper.name}"/>...
And in javascript,
function myFunc($val) {
alert($val);
}
passed the value
attribute with the function call as the parameter.
hope this helps!!
本文标签: JavaScript on JSP with Spring formselectStack Overflow
版权声明:本文标题:JavaScript on JSP with Spring form:select - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742066022a2418839.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论