首页 > web前端 > js教程 > jsp中执行onclick会刷新一次页面的问题

jsp中执行onclick会刷新一次页面的问题

PHPz
发布: 2018-10-10 15:19:19
转载
1931 人浏览过

今天做一个注册页面的时候,弄了一个清空的按钮,我执行清空按钮,但是按钮调用的方法 什么都没写,所有文本都清空了

代码如下:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>我要注册</title>
<script type="text/javascript">
function resetClick()
{
	
}


</script>



</head>
 <body style="text-align: center;">
        <form action="zhuche" method="post">
            <table width="40%" border="1">
                <tr>
                    <td>用户名</td>
                    <td>
                        
                        <input type="text" name="userName" id ="userName">
                    </td>
                </tr>
                <tr>
                    <td>密码</td>
                    <td>
                        <input type="password" name="userPwd" id="userPwd">
                    </td>
                </tr>
                <tr>
                    <td>确认密码</td>
                    <td>
                        <input type="password" name="confirmPwd" id="confirmPwd">
                    </td>
                </tr>
                <tr>
                    <td>
                    <!-- 执行重置  -->
                        <input type="reset" value="清空" onclick="resetClick()">
                    </td>
                    <td>
                        <input type="submit" value="注册">
                    </td>
                </tr>
            </table>
        </form>
    </body>
</html>
登录后复制

查了一下资料,原因是在  

执行onclick,所以每次页面都会刷新一次。

有两种方法,不用刷新页面

1、不用 格式,删掉这个格式

2、改成  onclick="return resetClick()" ,其中resetClick()为onclick方法,并且在方法中 return = false; 这样就不用刷新页面了

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>我要注册</title>
<script type="text/javascript">
function resetClick()
{
	document.getElementById("userName").value = "";
	document.getElementById("userPwd").value = "";
	document.getElementById("confirmPwd").value = "";
	return false;
}


</script>



</head>
 <body style="text-align: center;">
        <form action="zhuche" method="post">
            <table width="40%" border="1">
                <tr>
                    <td>用户名</td>
                    <td>
                        
                        <input type="text" name="userName" id ="userName">
                    </td>
                </tr>
                <tr>
                    <td>密码</td>
                    <td>
                        <input type="password" name="userPwd" id="userPwd">
                    </td>
                </tr>
                <tr>
                    <td>确认密码</td>
                    <td>
                        <input type="password" name="confirmPwd" id="confirmPwd">
                    </td>
                </tr>
                <tr>
                    <td>
                    <!-- 执行重置  -->
                        <input type="reset" value="清空" onclick="return resetClick()">
                    </td>
                    <td>
                        <input type="submit" value="注册">
                    </td>
                </tr>
            </table>
        </form>
    </body>
</html>
登录后复制

更多相关教程请访问 JavaScript视频教程

相关标签:
来源:csdn.net
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板