这是我的trialindex.php代码。该代码包含一个正确提交的html表单。我正在尝试创建一个javascript函数,以便在提交表单时调用。现在这个功能只有一个警报。但是在提交表单时不会调用该函数。
<?php session_start();?>
<html>
<head>
<script type="text/javascript">
function try(){
alert("Hello");
}
</script>
<title>Untitled Document</title>
</head>
<body>
<div id="change">
<form id="trialForm" method="post" action="connection.php" onSubmit="try()">
<center>
<h3><b>Login</b></h3>
<br/>
<label>Username :</label>
<input type="text" id="username" name="username"/>
<br/>
<br/>
<label>Password :</label>
<input type="password" id="password" name="password"/>
<br/>
<br/>
<input type="submit" value="Login"/>
</center>
</form>
</div>
</body>
</html>
javascript函数try()似乎不起作用。我也尝试了几个其他的尝试,如:try(); ,javascript:try(),javascript:try();
但在任何语法中都不会弹出警报。同样在我的浏览器弹出窗口中也没有被阻止。 你能否建议可能出现的问题是什么?
答案 0 :(得分:2)
您不能将try
用作功能名称
这是一个保留字:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Reserved_Words
只需为您的功能使用其他名称。
function my_submit_function(){
alert("Hello");
}
最好检查控制台是否有错误。
答案 1 :(得分:1)