if (document.getElementById("Text1").value.length==0)
{
return confirm('用户名不能为空!');
document.getElementById("Text1").value=="<font color='red'>用户名不能为空</font>";
return false;
}页面初始化时,有对文本框赋值,值为“”(空),脚本判断该文本框时,为什么没有进行判断呢?
{
return confirm('用户名不能为空!');
document.getElementById("Text1").value=="<font color='red'>用户名不能为空</font>";
return false;
}页面初始化时,有对文本框赋值,值为“”(空),脚本判断该文本框时,为什么没有进行判断呢?
解决方案 »
- 关于jQuery.ajax的一个小问题
- 表单怎么设计,能具有输入下划线的功能
- jquery 源码clean方法疑难问题请教
- IE6有问题,FF正常 什么原因
- 如何获取函数所处的<script标记的对象呢?
- 高分求教javascript靠右悬浮窗体显示问题
- 郁闷死了,如何提取如下格式的地址?????
- window.showModalDialog()可以打开一个空白窗口,并在新窗口内用document.write写东西吗?
- 请给我一个例:在页面a上,点一按钮,弹出页面b,在页面b中得输入筐输入文字,点按钮“ok”,关闭b,输入得文字显示在a得文本筐!
- 变量无法调用问题??请指点
- ext java 后台数据如何交互?
- 求一分页JS代码
if(ss.length==0)
{}
<!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>
<title> new document </title>
<meta name="generator" content="editplus" />
<meta name="author" content="[email protected]" />
<meta name="keywords" content="" />
<meta name="description" content="" />
</head>
<body>
<form method="post" action="">
<input type="text" id="userNameTxt" />
<input type="submit" />
</form>
<script type="text/javascript">
<!--
document.forms[0].onsubmit = function() {
var userName = document.getElementById("userNameTxt");
if (userName.value.length == 0)
{
alert("用户名不能为空!");
userName.value ="用户名不能为空";
userName.style.color = "red";
return false;
}
};
//-->
</script>
</body>
</html>
trim(document.getElementById("Text1").value).length
单单运行这行代码,有实现弹出文本框,所以判断document.getElementById("Text1").value.length==0这条语句判断失败
部分HTML代码如下:
<body>
<form id="form1" runat="server">
<!--startprint-->
<table class="table" enableviewstate="true" align="center" cellspacing="0" cellpadding="0" style="width: 800; text-align: left">
<tr>
<td class="td">
姓名
</td>
<td class="td">
<input id="Text1" type="text" runat="server" class="Text2" />
</td>
<td class="td">
英文名
</td>
<td class="td">
<input id="Text2" runat="server" class="Text2" type="text" />
</td>
</tr>
<input id="BtnTest" title="脚本测试" type="button" class="button" value="脚本测试"
onclick="if (confirm('确定测试?')){judgeUserInput();}"/>
脚本:
if (trim(document.getElementById("Text1").value).length==0)
{
confirm('用户名不能为空!'); //没有执行判断
document.getElementById("Text1").value=="<font color='red'>用户名不能为空</font>";
return false;
}
输入空格的话length不为0
alert("用户名" + userName);
弹出的内容是乱七八糟的符号,正方形三角形什么的
由于未提供 trim 代码,因此
trim(document.getElementById("Text1").value).length
改为
document.getElementById("Text1").value.length== 是比较是否相等,而非赋值!
document.getElementById("Text1").value=="<font color='red'>用户名不能为空</font>";
改为
document.getElementById("Text1").value="<font color='red'>用户名不能为空</font>";L@_@K
<!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>
<title> new document </title>
<meta name="generator" content="editplus" />
<meta name="author" content="[email protected]" />
<meta name="keywords" content="" />
<meta name="description" content="" />
</head>
<body>
<form id="form1" runat="server">
<!--startprint-->
<table class="table" enableviewstate="true" align="center" cellspacing="0" cellpadding="0" style="width: 800; text-align: left">
<tr>
<td class="td">
姓名
</td>
<td class="td">
<input id="Text1" type="text" runat="server" class="Text2" />
</td>
<td class="td">
英文名
</td>
<td class="td">
<input id="Text2" runat="server" class="Text2" type="text" />
</td>
</tr>
</table>
<input id="BtnTest" title="脚本测试" type="button" class="button" value="脚本测试"
onclick="if (confirm('确定测试?')){judgeUserInput();}"/>
</form>
<script type="text/javascript">
<!--
function judgeUserInput()
{
if (document.getElementById("Text1").value.length==0)
{
confirm('用户名不能为空!'); //没有执行判断
document.getElementById("Text1").value = "<font color='red'>用户名不能为空</font>";
return false;
}
}
//-->
</script>
</body>
</html>
以下为trim的代码:
//仅删除文本框中首尾的空格
function trim(s) {
var i;
for (i = 0; i < s.length && s.charCodeAt(i) == 32; i ++);
s = s.substring(i, s.length);
for (i = s.length - 1; i >= 0 && s.charCodeAt(i) == 32; i--);
s = s.substring(0, i + 1);
return s;
}
脚本是测试通过了,不过以下内容在HTML页面输出时有问题:
document.getElementById("Text1").value = "<font color='red'>用户名不能为空</font>";文本框显示内容为"<font color='red'>□□□□□□□□□</font>";而不是我希望得到的红色字体+非乱码文字
楼主有点粗心了,赋值语句用=
这样判断是否为空
设置字体为红色
因为 ANSI 格式无法正确编码中文!
http://download.csdn.net/source/308913JScript 语言参考
http://download.csdn.net/source/308916CCS 样式表中文手册
http://download.csdn.net/source/304124
<!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>
<title> new document </title>
<meta name="generator" content="editplus" />
<meta name="author" content="[email protected]" />
<meta name="keywords" content="" />
<meta name="description" content="" />
</head>
<body>
<!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>
<title> new document </title>
<meta name="generator" content="editplus" />
<meta name="author" content="[email protected]" />
<meta name="keywords" content="" />
<meta name="description" content="" />
</head>
<body>
<form id="form1" runat="server">
<!--startprint-->
<table class="table" enableviewstate="true" align="center" cellspacing="0" cellpadding="0" style="width: 800; text-align: left">
<tr>
<td class="td">
姓名
</td>
<td class="td">
<input id="Text1" type="text" runat="server" class="Text2" />
</td>
<td class="td">
英文名
</td>
<td class="td">
<input id="Text2" runat="server" class="Text2" type="text" />
</td>
</tr>
</table>
<input id="BtnTest" title="脚本测试" type="button" class="button" value="脚本测试"
onclick="if (confirm('确定测试?')){judgeUserInput();}"/>
</form>
<script type="text/javascript">
<!--
var requireUsernameAlert = "用户名不能为空!";
var chineseUserName = document.getElementById("Text1");
function judgeUserInput()
{
if (chineseUserName.value.length==0)
{
alert(requireUsernameAlert);
chineseUserName.value = requireUsernameAlert;
chineseUserName.style.color = "red";
return false;
}
}
chineseUserName.onfocus = function() {
if (this.value == requireUsernameAlert)
{
this.value = "";
this.style.color = "black";
}
};
//-->
</script>
</body>
</html></body>
</html>
document.getElementById("Text1").value="<font color='red'>用户名不能为空</font>";
两个等于号做什么???
if (this.value == requireUsernameAlert)
{
this.value = "";
this.style.color = "black";
}
};
没有触发,不知道为什么
function judgeUserInput()
{
var userName = document.getElementById("Text1");
alert("用户名" + userName.value);
if (trim(document.getElementById("Text1").value).length ==0)
{
document.getElementById("Text1").value="用户名不能为空";
document.getElementById("Text1").style.color= "red";
}
}
//清除文本框中的错误信息
document.getElementById("Text1").onfocus = function() {
alert("用户名清除"); //没有执行
if (trim(this.value) == "用户名不能为空")
{
this.value = "";
this.style.color = "black";
}
};
{
document.getElementById("Text1").value="";
document.getElementById("Text1").style.color= "black";
}
Encoding myEncoding = Encoding.GetEncoding("gb2312");
Response.ContentEncoding = myEncoding;
乱码就不复存在咯!
页面中包含一个用户自定义控件:
<td class="td">
<uc1:My97DatePicker ID="begdate" class="Text2" runat="server" Format="Date" />
</td>
如何判断当日期没有设置时,设置焦点在该控件上?
document.getElementById("begdate").focus();//没有效果
document.getElementById("begdate").focus();
}