Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
159 views
in Technique[技术] by (71.8m points)

html - javascript to jsp

How to show a javascript 'var' in my jsp?

 ...
 <script type="text/javascript">
 ... // My code to get the value. 
 var val = combo.getValue(); 
 </script>
 <body>
 The value is : //to be displayed here
 </body>
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Add a HTML element which should mark the place where the value is to be displayed and give it an id.

<body>
    The value is : <span id="value"></span>
</body>

Then let your JS access it by document.getElementById() and modify its inner HTML.

document.getElementById("value").innerHTML = val;

You only need to ensure that the particular script executes after the HTML page has loaded. Do it during window.onload or put the <script> at end of <body> or wrap it in a function which you execute on some event.

As to the JSP part, this is not relevant here. All JSP does is generating and sending HTML/CSS/JS code from webserver to webbrowser. JavaScript knows nothing about JSP, all it can see and access is HTML/CSS which you can also see by rightclicking the page in webbrowser and choosing View Source.

See also:


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...