JavaScript 表单提交的三种方法

表单提交的三种方法:
1、submit按钮,结合form标签的onsubmit事件。  <form name="form1" method="post" action="login.php" οnsubmit="return checkForm()" > </form> checkForm()需要return
2、submit按钮,结合onclick事件。  <input type="submit" value="提交表单" οnclick="return checkForm()" />  checkForm()需要return
3、button按钮,结合onclick事件。 <input type="button" value="提交表单" οnclick="return checkForm()" />  js中:checkForm(){document.form1.submit();} 不需要return


1、submit按钮,结合form标签的onsubmit事件:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
<script type="text/javascript">
//实例:submit按钮,结合onsubmit事件,验证和提交表单
function checkForm()
{
	//判断用户名是否为空,需要return
	if(document.form1.username.value=="")
	{
		window.alert("用户名不能为空!");
		return false;
	}else if(document.form1.username.value.length<5 || document.form1.username.value.length>20)
	{
		window.alert("用户名长度必须介于5-20个字符之间!");
		return false;
	}
}
</script>
</head>
<body>

<form name="form1" method="get" action="login.php">
用户名:<input type="text" name="username" />
密码:<input type="password" name="userpwd" />
<input type="submit" value="提交表单" οnsubmit="return checkForm()" />
</form>


</body>
</html>
2、submit按钮,结合onclick事件:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
<script type="text/javascript">
//实例:submit按钮,结合onclick事件,验证和提交表单
function checkForm()
{
	//判断用户名是否为空,需要return
	if(document.form1.username.value=="")
	{
		window.alert("用户名不能为空!");
		return false;
	}else if(document.form1.username.value.length<5 || document.form1.username.value.length>20)
	{
		window.alert("用户名长度必须介于5-20个字符之间!");
		return false;
	}
}
</script>
</head>
<body>
<form name="form1" method="get" action="login.php">
用户名:<input type="text" name="username" />
密码:<input type="password" name="userpwd" />
<input type="submit" value="提交表单" οnclick="return checkForm()" />
</form>

</body>
</html>
3、button按钮,结合onclick事件:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
<script type="text/javascript">
//实例:button按钮,结合onclick事件,验证和提交表单
function checkForm()
{
	//判断用户名是否为空,不需要return
	if(document.form1.username.value=="")
	{
		window.alert("用户名不能为空!");
	}else if(document.form1.username.value.length<5 || document.form1.username.value.length>20)
	{
		window.alert("用户名长度必须介于5-20个字符之间!");
	}else
	{
		//使用form对象的submit()方法,实现提交,不用return
		document.form1.submit();
	}
}
</script>
</head>
<body>
<form name="form1" method="get" action="login.php">
用户名:<input type="text" name="username" />
密码:<input type="password" name="userpwd" />
<input type="button" value="提交表单" οnclick="checkForm()" />
</form>

</body>
</html>




  • 6
    点赞
  • 49
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
你可以使用 HTML 表单JavaScript 来实现表单提交。下面是一个简单的例子,假设你有一个包含用户名和密码输入框的表单: ```html <form id="my-form"> <label>用户名:</label> <input type="text" name="username"><br> <label>密码:</label> <input type="password" name="password"><br> <input type="submit" value="提交"> </form> ``` 然后使用 JavaScript 代码来拦截表单提交事件,并通过 AJAX 发送表单数据到服务器端: ```javascript document.getElementById("my-form").addEventListener("submit", function(event) { event.preventDefault(); // 阻止表单默认提交行为 var xhr = new XMLHttpRequest(); xhr.open("POST", "/submit-form"); // 替换成服务器端接口地址 xhr.setRequestHeader("Content-Type", "application/json"); // 设置请求头 xhr.onreadystatechange = function() { if (xhr.readyState === 4 && xhr.status === 200) { console.log(xhr.responseText); // 在控制台输出响应结果 } }; xhr.send(JSON.stringify({ username: document.getElementsByName("username")[0].value, password: document.getElementsByName("password")[0].value })); // 将表单数据转换为 JSON 字符串并发送到服务器端 }); ``` 上面的代码中,我们使用了 `preventDefault()` 方法来阻止表单的默认提交行为,然后创建了一个 XMLHttpRequest 对象,设置请求头和回调函数,并使用 `send()` 方法表单数据发送到服务器端。在服务器端,你需要解析请求体中的 JSON 数据并进行相应的处理。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值