1 <!DOCTYPE html> 2 <html> 3 <head> 4 <title>index</title> 5 </head> 6 <body> 7 <form name="myform" id="myform" target="_self"> 8 帐号:<input type="text" name="username"><br> 9 密码:<input type="password" name="password"><br> 10 <input type="submit" value="登录" name="login" onclick="login()" > 11 <input type="submit" value="注册" name="register" onclick="register()" > 12 </form> 13 14 <script type="text/JavaScript"> 15 function register() { 16 alert("register") 17 document.getElementById("myform").action="/register"; 18 document.getElementById("myform").submit(); 19 } 20 function login() { 21 alert("login") 22 document.getElementById("myform").action="/login"; 23 document.getElementById("myform").submit(); 24 } 25 </script> 26 27 </body> 28 </html>
失效代码
在写前台JS的时候,id和函数名不能一致,否则就会导致onclick失效,修改了函数的名字,问题解决!
1 <!DOCTYPE html> 2 <html> 3 <head> 4 <title>index</title> 5 </head> 6 <body> 7 <form name="myform" id="myform" target="_self"> 8 帐号:<input type="text" name="username"><br> 9 密码:<input type="password" name="password"><br> 10 <input type="submit" value="登录" name="login" onclick="funlogin()" > 11 <input type="submit" value="注册" name="register" onclick="funregister()" > 12 </form> 13 14 <script type="text/JavaScript"> 15 function funregister() { 16 alert("register") 17 document.getElementById("myform").action="/register"; 18 document.getElementById("myform").submit(); 19 } 20 function funlogin() { 21 alert("login") 22 document.getElementById("myform").action="/login"; 23 document.getElementById("myform").submit(); 24 } 25 </script> 26 27 </body> 28 </html>
修改后的代码
时间: 2024-10-06 23:29:01