87,989
社区成员
发帖
与我相关
我的任务
分享function validata()
{
var str=document.getElementById('Uname').value;
if (str == "")
{
alert('yourName!!!!');
document.getElementById('Uname').focus();
}
else
{
alert("well com !!");
}
}
<form id="myForm" name="myForm" >
Name : <input type="text" id="Uname" size="30" />
<input type="button" id="Button" name="Button" value="button" onclick="validata()"/>
</form><form id="myForm" name="myForm" >
Name : <input type="text" id="Uname" size="30" />
<input type="button" id="Button" name="Button" value="button" />
</form>
<script type="text/javascript">
document.getElementById('Button').onclick = validata; //给id为Button的元素 加onclick事件
function validata()
{
var str = document.getElementById('Uname').value;
if (str == "")
{
alert('yourName!!!!');
document.getElementById('Uname').focus();
}
else
{
alert("well com !!");
}
}
</script><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
</head>
<body>
<form id="myForm" name="myForm" >
Name : <input type="text" id="Uname" size="30" />
<input type="button" id="Button" name="Button" value="button" />
</form>
<script>
document.getElementById('Button').onclick = validata;
function validata()
{
var str=document.getElementById('Uname').value;
if (str == "")
{
alert('yourName!!!!');
document.getElementById('Uname').focus();
}
else
{
alert("well com !!");
}
}
</script>
</body>
</html>
document.getElementById('Button').onclick = function()
{
var str=document.getElementById('Uname').value;
if (str == "")//提个建议,楼主这儿验证值得注意,如果我单纯输入空格怎么办?算"合理"?
{
alert('yourName!!!!');
document.getElementById('Uname').focus();
}
else
{
alert("well com !!");
}