javascript 如何通过jsp表达式标签将java字符串变量传递给javascript函数?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29197577/
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-28 10:13:26  来源:igfitidea点击:

How to pass java string variable to the javascript function by jsp expression tag?

javajavascriptjsp

提问by Aditya

I want to passing a java String variable to the javascript function parameter using jsp expression tag.Below is my jsp page.

我想使用 jsp 表达式标记将 java String 变量传递给 javascript 函数参数。下面是我的 jsp 页面。

First.jsp

首先.jsp

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
        <title>Insert title here</title>
        <script>
            function stringGenerate(str){
                var x = document.getElementsByTagName("input");
                x[0].value = str;
            }
            function numberGenerate(num){
                var x = document.getElementsByTagName("input");
                x[1].value = num;
            }
        </script>
    </head>
    <body>
        <%
            String num ="1234567890";
            String str = "abcdefghij";
        %>

        <input type="text" name="string" readonly="readonly"/> &nbsp;&nbsp;
        <input type="text" name="number" readonly="readonly"/><br/><br/>

        <input type="button" value="String Generate" onclick= "stringGenerate(<%=str %>)" /> &nbsp;&nbsp;
        <input type="button" value="Number Generate" onclick= "numberGenerate(<%=num %>)" />
    </body>
</html>

When I click on the button with value "Number Generate",then the num variable value(1234567890) will display on the textbox(name="number") but when I click on the button with value "String Generate",then there is nothing display on the corresponding text box(name="string").Here both num and str are string type varible but why only num variable value is displayed on textbox and why not str variable value is displayed?

当我单击值为“Number Generate”的按钮时,num 变量值(1234567890)将显示在文本框(名称=“数字”)上,但是当我单击值为“String Generate”的按钮时,则有对应的文本框什么都不显示(name="string")。这里num和str都是字符串类型变量,但是为什么textbox上只显示num变量值,为什么不显示str变量值呢?

采纳答案by Manwal

Try using single quote 'when using string in HTML:

'在 HTML 中使用字符串时尝试使用单引号:

onclick= "stringGenerate('<%=str %>')"
                         ^        ^

Full Code:

完整代码:

<input type="button" value="String Generate" onclick= "stringGenerate('<%=str %>')" />

回答by Dimitri Grishin

The reason is that when the page is rendered by the browser, it puts the str and num values directly in the code, so it looks like:

原因是浏览器渲染页面的时候,直接把str和num的值放在了代码中,所以看起来是这样的:

<input type="button" value="String Generate" onclick="stringGenerate(abcdefghij)"/>

The browser basically thinks you're trying to reference a Javascript variable called 'abcdefghij'.

浏览器基本上认为您正在尝试引用一个名为 .js 的 Javascript 变量'abcdefghij'

You should add the 'chars before and after your string text to let javascript know it should use the value of that text and not search for a variable with that name.

您应该'在字符串文本之前和之后添加字符,让 javascript 知道它应该使用该文本的值,而不是搜索具有该名称的变量。

The declaration of str should look like this to make it work:

str 的声明应如下所示以使其工作:

String str = "\'abcdefghij\'";

Please note, you can't use \"to escape as this would break your code.

请注意,您不能使用\"转义,因为这会破坏您的代码。