[英]jsp onclick not calling javascript function
I have looked at many other topics on here that are similar to my problem but none of the answers work for me. 我在这里查看了许多其他问题,这些问题与我的问题类似,但没有答案对我有用。 I am trying to call a javascript function when a button is clicked but it does nothing.
我试图在单击按钮时调用javascript函数,但没有任何作用。 I hvae tried changing the call to
onclick="javascript:ClearFields();"
我尝试将呼叫更改为
onclick="javascript:ClearFields();"
and onclick="ClearFields()"
and it doesn't work either. 和
onclick="ClearFields()"
,它也不起作用。 I have to use a button (not submit) calling javascript My jsp code is: 我必须使用调用javascript的按钮(不提交),我的jsp代码是:
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Calculator</title>
</head>
<body>
<!-- if the user is logged in then we need to render one version of the page
consequently if the user is logged out we need to render a
different version of the page -->
<script type="text/javascript">
function ClearFields() {
document.getElementById("num1").value = "";
document.getElementById("num2").value = "";
document.getElementById("operator").value = "+";
}
</script>
<c:choose>
<c:when test="${empty user}">
<p>
${msg} <br/>
Would you like to log in? <br/>
<a href="${login_url}">Sign in or register</a> <br/>
</p>
</c:when>
<c:otherwise>
<p>
Welcome ${user.email}
<p/>
<p><h1>Calculator</h1></p>
<!-- form of basic input types -->
<form action="/" method="post">
<table border="0">
<tr><td>
Number 1: <input type="text" name="num1" value="${ans}"/><br/>
</td><td>
Number 2: <input type="text" name="num2" /><br/>
</td></tr>
<tr><td colspan=2 align=center>
<select name="operator">
<option value="+"> + </option>
<option value="-"> - </option>
<option value="*"> * </option>
<option value="/"> / </option>
</select>
</td></tr>
<tr><td align=center>
<!-- Button to submit the form to the servlet when clicked -->
<input type="submit" value="Calculate"/><br/>
</td>
<td align=center>
<input type="button" onclick="ClearFields();" value="Clear"/><br/>
<!-- <input type="button" onclick="ClearFields();" value="Clear"/><br/> -->
</td></tr>
<tr><td colspan=2>
<h1> ${msg}</h1>
</td></tr>
</table>
</form>
<p>
You can signout <a href="${logout_url}">here</a>
</p>
</c:otherwise>
</c:choose>
</body>
</html>
The generated html is 生成的html是
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Calculator</title>
</head>
<body>
<!-- if the user is logged in then we need to render one version of the page
consequently if the user is logged out we need to render a
different version of the page -->
<script type="text/javascript">
function ClearFields() {
document.getElementById("num1").value = "";
document.getElementById("num2").value = "";
document.getElementById("operator").value = "+";
}
</script>
<p>
Welcome test@example.com
<p/>
<p><h1>Calculator</h1></p>
<!-- form of basic input types -->
<form action="/" method="post">
<table border="0">
<tr><td>
Number 1: <input type="text" name="num1" value=""/><br/>
</td><td>
Number 2: <input type="text" name="num2" /><br/>
</td></tr>
<tr><td colspan=2 align=center>
<select name="operator">
<option value="+"> + </option>
<option value="-"> - </option>
<option value="*"> * </option>
<option value="/"> / </option>
</select>
</td></tr>
<tr><td align=center>
<!-- Button to submit the form to the servlet when clicked -->
<input type="submit" value="Calculate"/><br/>
</td>
<td align=center>
<input type="button" onclick="ClearFields();" value="Clear"/><br/>
</td></tr>
<tr><td colspan=2>
<h1> Welcome to the Calculator</h1>
</td></tr>
</table>
</form>
<p>
You can signout <a href="/_ah/logout?continue=%2F">here</a>
</p>
</body>
</html>
Any ideas where I am going wrong? 有什么想法我要去哪里吗? Could it just be that the page is no resubmitting?
可能是该页面没有重新提交吗? It seems to be.
好像是。
Your JavaScript: 您的JavaScript:
document.getElementById("num1").value = "";
Your HTML: 您的HTML:
<input type="text" name="num1" value=""/>
getElementById
gets an element by its id , and your element doesn't have any id. getElementById
通过其id获取元素,并且您的元素没有任何id。 You need to add an id attribute. 您需要添加一个id属性。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.