Html 在 JSP 中调用 Javascript 函数

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

calling Javascript function in JSP

javascripthtmljsp

提问by newbie

I'm trying to call a function and activate an alert through it in my JSP.
This is what i've done so far:

我试图在我的 JSP 中调用一个函数并通过它激活一个警报。
这是我到目前为止所做的:

<html>
<head>
<script type="text/javascript">
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">

function myFunction( test )
{
    alert( test );
}
</script>
<title>Success</title>
</head>
<body>
     <c:set var="test" scope="request" value="${requestScope.userDetails }"></c:set>
     <input type="button" id="sample_button" onclick="myFunction(${test.userName})" value="test">
</body>
</html>

What is wrong with my code?

我的代码有什么问题?

采纳答案by vjy

In addition to that, You might also need to add a single quote inside the calling function

除此之外,您可能还需要在调用函数中添加单引号

<input type="button" id="sample_button" onclick="myFunction('${test.userName}')" value="test">

Check for javascript errors & verify the generated HTML in browser

检查javascript错误并验证浏览器中生成的HTML

回答by Arun P Johny

You had a metaelement inside scriptelement which will throw an error while parsing the script block

您在metaelement 中有一个元素,script它会在解析脚本块时抛出错误

<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"/>
        <script type="text/javascript">

            function myFunction( test )
            {
            alert( test );
            }
        </script>
        <title>Success</title>
    </head>
    <body>
        <c:set var="test" scope="request" value="${requestScope.userDetails }"></c:set>
        <input type="button" id="sample_button" onclick="myFunction(${test.userName})" value="test" />
    </body>
</html>