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
calling Javascript function in JSP
提问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 meta
element inside script
element which will throw an error while parsing the script block
您在meta
element 中有一个元素,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>