- 主题:表单js求助
功能很简单,就是想判断某个字段没填不提交表单并且显示提示
可是试了一个页面work,同样的试验另一个页面却不行,调试了半天不知道问题出在哪里
代码如下:
js:
function text()
{
if (document.text.textid.value.length < 1 )
{
show('errtextid');
text.textid.focus();
return false;
} else {
hide('errtextid');
return true;
}
}
function seq()
{
if (document.seq.seqid.value.length < 1 )
{
show('errseqid');
seq.seqid.focus();
return false;
} else {
hide('errseqid');
return true;
}
}
function text()
{
if (document.text.textid.value.length < 1 )
{
show('errtextid');
text.textid.focus();
return false;
} else {
hide('errtextid');
return true;
}
}
function seq()
{
if (document.seq.seqid.value.length < 1 )
{
show('errseqid');
seq.seqid.focus();
return false;
} else {
hide('errseqid');
return true;
}
}
function show(sh)
{
obj=document.getElementById(sh)
obj.style.display == "block" ? obj.style.display = "block" : obj.style.display = "block";
}
function hide(sh)
{
obj=document.getElementById(sh)
obj.style.display == "none" ? obj.style.display = "none" : obj.style.display = "none";
}
html部分,php
<form action="search_text.php" id="text" method="get" name="search" target="_self" onsubmit="return text();">
<p class="tag2"> <input id="textid" name="searchtext" type="text" size="60"> <span class="sh" id="errtextid">Please input some keywords for search left~~</span></p>
<p class="tag2"> <input name="" type="submit" value="Search for me"> <input name="" type="reset" value="Clear Input"></p>
</form>
<form action="search_seq.php" id="seq" method="post" name="search" target="_self" onsubmit="return seq();">
<p class="tag2"> <textarea id="seqid" name="searchseq" cols="80" rows="8"></textarea></p>
<p class="tag2"> <input name="" type="submit" value="Blast for me"> <input name="" type="reset" value="Clear Input"> <span class="sh" id="errseqid">Please paste an sequence above~~</span></p>
</form>
比较乱~~
谢谢了:)
--
FROM 211.99.222.*
【 在 lidaof (lidaof) 的大作中提到: 】
: 功能很简单,就是想判断某个字段没填不提交表单并且显示提示
: 可是试了一个页面work,同样的试验另一个页面却不行,调试了半天不知道问题出在哪里
: 代码如下:
: ...................
用firebug之类调试啊,贴代码乱的。
--
FROM 211.99.222.*
没用过- -
下面的代码在firefox下不能拦住表单,不知道应该怎么改了
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<title>Validation hide show test</title>
<meta http-equiv="content-type" content="text/html; charset=ISO-8859-1" />
<script type="text/javascript">
<!--
function show(sh)
{
obj=document.getElementById(sh)
obj.style.display == "block" ? obj.style.display = "block" : obj.style.display = "block"; // {
}
function hide(sh)
{
obj=document.getElementById(sh)
obj.style.display == "none" ? obj.style.display = "none" : obj.style.display = "none"; // {
}
function validate()
{
if (document.myform.username.value == "")
{
show('s1');
return false;
} else {
hide('s1');
return true;
}
if (document.myform.city.value == "")
{
show('s2');
return false;
} else {
hide('s2');
return true;
}
}
//-->
</script>
<style type="text/css">
.sh {
display: none; position: relative;
}
</style>
</head>
<body>
<form name="myform" id="myform" method="get"
action="http://www.javascriptref.com/"
onsubmit="return validate();">
Username:
<input type="text" name="username" id="username" size="30" />
<br />
<span id="s1" class="texto : sh">Username missing!</span>
<br />
<br />
City:
<input type="text" name="city" id="city" size="30" />
<br />
<span id="s2" class="texto : sh">City missing!</span>
<br />
<br />
<input type="submit" value="submit" />
</form>
</body>
</html>
【 在 bombard (6斤宝宝,要健康哦!) 的大作中提到: 】
: 用firebug之类调试啊,贴代码乱的。
--
FROM 211.99.222.*
首先,不要搞两个name相同的input,容易把自己和浏览器都搞晕。
其次,看看你的程序:
function validate()
{
if (document.myform.username.value == "")
{
show('s1');
return false;
} else {
hide('s1');
return true;
}
if (document.myform.city.value == "")
{
show('s2');
return false;
} else {
hide('s2');
return true;
}
}
username不为空的时候直接返回true了。后边检查city的程序段根本执行不到了。
不会用firebug没关系,但至少要用alert来调试javascript。
【 在 lidaof (lidaof) 的大作中提到: 】
: 没用过- -
: 下面的代码在firefox下不能拦住表单,不知道应该怎么改了
: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
: ...................
--
FROM 222.248.228.*
看了你的提示才发现我把id和name搞混了。。。尴尬
不过为啥return false了firefox却还要执行表单。。。ie倒是正常- -
function stext()
{
if (document.searcht.searchtext.value == "")
{
show('errtextid');
//searcht.searchtext.focus();
return false;
} else {
hide('errtextid');
return true;
}
}
把focus那名注释掉firefox也好使了....太奇怪了- -
【 在 WTO (好五倍) 的大作中提到: 】
: 首先,不要搞两个name相同的input,容易把自己和浏览器都搞晕。
: 其次,看看你的程序:
: function validate()
: ...................
--
修改:lidaof FROM 211.99.222.*
FROM 211.99.222.*