Grails using grails var in GSP Site inside javascript

安稳与你 提交于 2019-12-06 02:03:44

问题


I have a question using grails variable values in javascript code in a GSP file.

For Example: I have a session value session.getAttribute("selectedValue") and I want to use this value inside javascript code part.

My solution is now (inside a GSP):

<%
    def js = new String("<script type=\"text/javascript\">")
    js += "var jsSelectedValue = " + session.getAttribute("selectedValue") + ";"
    js += "</script>"
    out << js
%>

and then I have javascript block inside my GSP with jQuery Stuff and so on, there I need this value.

Is there another way to have grails variables accessible inside pure javascript code?

And second question, the exactly other way around. I select for example in a dropdown box and click "save" and then i want to store the value $("#select-box").val() inside a session variable from JS-part.

Thank you very much in advance for your help.

Cheers,

Marco


回答1:


Why do not use the javascript GSP-tag? A solution can look like this:

<g:javascript>
    var jsSelectedValue = "${session.selectedValue}"; 
</g:javascript>



回答2:


The solution to your first problem might be as follows:

UPDATE: Modifications according to @Medrod's solution:

<script type="text/javascript">
var jsSelectedValue = "${session.selectedValue}";
</script>

And for second question:
Send selected value to server and set session variable.



来源:https://stackoverflow.com/questions/6995941/grails-using-grails-var-in-gsp-site-inside-javascript

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!