调用JSP中的Javascript函数

calling Javascript function in JSP

本文关键字:函数 Javascript 中的 JSP 调用      更新时间:2023-09-26

我正试图在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>

我的代码出了什么问题?

您在script元素中有一个meta元素,在解析脚本块时会引发错误

<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>

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

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

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