admin管理员组文章数量:1410730
I am new here. I am trying to store a JavaScript variable in a session by using hidden fields and submitting a form:
function populateInput()
{
rowClicked = true;
var key = document.getElementById("key").innerText;
var value = document.getElementById("value").innerText;
document.getElementById("key2").value = key;
document.getElementById("value2").value = value;
var form = document.getElementById("output");
form.submit();
}
which will be called by:
<html>
<tr id="row" onclick="populateInput()"
</html>
The object is to take the values from a row, which contains two columns, a key and a value (stored in a HashMap), and pass them to another jsp, so that they can be displayed in two text boxes (one for key and one for value) by clicking a row on another jsp.
The trouble I am having is with setting the attribute within scriptlets. I am getting the innerText of a td element, and assigning it to a variable in JavaScript and then passing it to the value of an HTML DOM object.
Then I want to submit this to another form using scriptlets (<%request.setAttribute%>), but how? I realize that by using scriptlets, you're using Java code, which is not an HTML attribute.
Any help would be highly appreciated.
Here is the whole table:
<%if (m != null){%>
<%for (Map.Entry<String, String> e : m.entrySet()){ %>
<tr id="row" onclick="populateInput()">
<td id="key"><%=e.getKey() %></td>
<td id="value"><%=e.getValue() %></td>
</tr>
<%} %>
<%} %>
I want to get the innerText of the td elements, store them into the vars, key, and value, and then store those vars into the value of:
<input type="hidden" id="key2">
<input type="hidden" id="value2">
The trouble I am having is with setting the value of the hidden fields using request.setAttribute("", obj) in scriptlets, and submitting them to the other form.
I am new here. I am trying to store a JavaScript variable in a session by using hidden fields and submitting a form:
function populateInput()
{
rowClicked = true;
var key = document.getElementById("key").innerText;
var value = document.getElementById("value").innerText;
document.getElementById("key2").value = key;
document.getElementById("value2").value = value;
var form = document.getElementById("output");
form.submit();
}
which will be called by:
<html>
<tr id="row" onclick="populateInput()"
</html>
The object is to take the values from a row, which contains two columns, a key and a value (stored in a HashMap), and pass them to another jsp, so that they can be displayed in two text boxes (one for key and one for value) by clicking a row on another jsp.
The trouble I am having is with setting the attribute within scriptlets. I am getting the innerText of a td element, and assigning it to a variable in JavaScript and then passing it to the value of an HTML DOM object.
Then I want to submit this to another form using scriptlets (<%request.setAttribute%>), but how? I realize that by using scriptlets, you're using Java code, which is not an HTML attribute.
Any help would be highly appreciated.
Here is the whole table:
<%if (m != null){%>
<%for (Map.Entry<String, String> e : m.entrySet()){ %>
<tr id="row" onclick="populateInput()">
<td id="key"><%=e.getKey() %></td>
<td id="value"><%=e.getValue() %></td>
</tr>
<%} %>
<%} %>
I want to get the innerText of the td elements, store them into the vars, key, and value, and then store those vars into the value of:
<input type="hidden" id="key2">
<input type="hidden" id="value2">
The trouble I am having is with setting the value of the hidden fields using request.setAttribute("", obj) in scriptlets, and submitting them to the other form.
Share Improve this question edited Oct 4, 2012 at 3:44 Braz Mann asked Oct 4, 2012 at 3:19 Braz MannBraz Mann 251 gold badge1 silver badge5 bronze badges 2- 1 Scriptlets run on the server. JavaScript runs in the browser. How exactly are you trying to "submit this to another form?" Show an SSCCE please. – Matt Ball Commented Oct 4, 2012 at 3:23
- Oy, that's pletely unreadable. As a general rule, please edit your question instead of menting on it – especially when adding code. – Matt Ball Commented Oct 4, 2012 at 3:36
2 Answers
Reset to default 2You CANNOT set a Session using JavaScript the reason being as explained in Matt Ball's ment.
You can either
- Use
AJAX
and send required data usingPOST/GET
method and then set it to the session. - Or You can use a
cookie
.
You cannot store a session variable directly from javascript because you can only set the value of session in the server side.So you need some AJAX to municate with the server from javascript and then manipulate the session value.
本文标签: htmlHow to store a JavaScript variable in session using scriptletsStack Overflow
版权声明:本文标题:html - How to store a JavaScript variable in session- using scriptlets - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745038087a2638908.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论