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 !!");
}
}<form id="myForm" name="myForm" >
Name : <input type="text" id="Uname" size="30" />
<input type="button" id="Button" name="Button" value="button" />
</form>document.getElementById('Button').onclick = validata; 这条语句应该怎么理解啊。
为什么当 Button click 时候不去执行 validata() 啊。
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" />
</form>document.getElementById('Button').onclick = validata; 这条语句应该怎么理解啊。
为什么当 Button click 时候不去执行 validata() 啊。
Button click 时候 validata()还是没有执行。
<head>
<title></title>
<script>
function validata() {
var str = document.getElementById('Uname').value;
if (str == "") {
alert('yourName!!!!');
document.getElementById('Uname').focus();
}
else {
alert("well com !!");
}
} </script>
</head>
<body>
<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></body>
</html>
是不是想要这种效果啊
我们 tl 说可以不用在 HTML 标签里 用onclick 可以实现。
<!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>
把js代码放到最下面~·
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>
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>
document.getElementById('Button').onclick = function()
{
var str=document.getElementById('Uname').value;
if (str == "")//提个建议,楼主这儿验证值得注意,如果我单纯输入空格怎么办?算"合理"?
{
alert('yourName!!!!');
document.getElementById('Uname').focus();
}
else
{
alert("well com !!");
}
里面详细代码我没写出来那些正则表达式什么的,这些代码只是一个测试用的。