javascript 在javascript文件中获取jsp值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16154713/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 03:28:58  来源:igfitidea点击:

Getting jsp value in javascript file

javascriptjsp

提问by bdfios

I have the following code in abc.jsp:

我在 abc.jsp 中有以下代码:

<%@page import="soundcap.Soundcap"%>

<%

    Soundcap cfl = new Soundcap();
    var sfl = cfl.playFile();

%>

I need the value of sfl in an external javascript file (jcode.js). How can I get this value (sfl) from jsp in javascript?

我需要外部 javascript 文件 (jcode.js) 中的 sfl 值。如何从javascript中的jsp获取这个值(sfl)?

回答by rahul maindargi

use this...

用这个...

<%
//get your sfl
 %>
  <input type="hidden" id="sfl" value="<%=sfl%>">

in your js file use

在你的 js 文件中使用

var sfl=document.getElementById("sfl").value; 

回答by zygimantus

Just place your JSP value in input tag like this:

只需将您的 JSP 值放在输入标签中,如下所示:

<input type="hidden" id="value" value="${value}">

And get this value in JS like this:

并在 JS 中获取此值,如下所示:

var value = document.getElementById("value").value;

回答by 11684

Very simple:

很简单的:

<% 
//get your sfl
%>

<script>
var xyz = <% out.print(sfl); %>;
</script>