提交表单首先检查有关内容是不是为空,我检查下拉列表框的值为什么总是空呢?

woshihuzi 2005-04-26 07:34:51
我的提交页面代码:1.htm
================================
<html>
<script language="Javascript">
<!--
function chk_form() {
alert("姓名:"+document.form_test.yourname.value+"\n爱吃的食物:"+document.form_test.food.value);
if (document.form_test.yourname.value=="")
alert("姓名不能为空。");
else if (document.form_test.food.value=="")
alert("爱吃的食物不能为空。");
else
{
document.form_test.submit();
}
}
-->
</script>
<body>
<form method="POST" name="form_test" action=2.asp>
姓名:<input type="text" name="yourname"><br>
爱吃的食物:<select size="1" name="food">
<option>香蕉</option>
<option>苹果</option>
</select><br>
<input type="button" OnClick="chk_form();" value="提交" name="B1">
</form>
</body>
</html>

接受页面的代码:2.asp
================================
<%
dim name,food
name = Trim(Request.Form("yourname"))
food = Trim(Request.Form("food"))
%>
<html>
<body>
您的姓名:<%=name%><br>
您爱吃的食物:<%=food%>
</body>
</html>

----------------------------------------------------------
遇见的问题:
姓名文本框输入“张三”,下拉列表框选择“苹果”,提交不成功。屏幕上说:“爱吃的食物不能为空。”
如果修改代码,把如下两句屏蔽:
// else if (document.form_test.food.value=="")
// alert("爱吃的食物不能为空。");
呵呵,这时候,虽然提示信息告诉我,姓名是张三,爱吃的食物是空字符串,但是能提交成功,结果显示如下:
您的姓名:张三
您爱吃的食物:苹果
这是怎么回事呢?我该怎样做才能让屏幕告诉我,爱吃的食物是苹果呢?


...全文
306 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
Accelerator 2005-04-27
  • 打赏
  • 举报
回复
<!--我按照最后一个改了一下:-->
<html>
<script language="Javascript">
<!--
function chk_form() {
if (document.form_test.yourname.value=="")
{
alert("姓名不能为空。");
return false;
}
if (document.form_test.food.value=="null")
{
alert("爱吃的食物不能为空。");
return false;
}
return true;

}
-->
</script>
<body>
<form method="POST" name="form_test" action=2.asp onsubmit="return chk_form()">
姓名:<input type="text" name="yourname"><br>
爱吃的食物:<select size="1" name="food">
<option value="null"></option>
<option value="香蕉">香蕉</option>
<option value="苹果">苹果</option>
</select><br>
<input type="submit" value="提交" name="B1" onclick="return chk_form();">
</form>
</body>
</html>
Accelerator 2005-04-27
  • 打赏
  • 举报
回复
首先是这段代码:
if (document.form_test.yourname.value=="")
alert("姓名不能为空。");
return false;
差括号,如上的话函数永远返回false,加上花括号,if代码段多于一条语句时就要用花括号了。如下:
if (document.form_test.yourname.value=="")
{
alert("姓名不能为空。");
return false;
}

其次是这段:
else if (document.form_test.food.options[document.form_test.food.selectedIndex].value=="")
alert("爱吃的食物不能为空。");
return false;
道理同上,加上花括号

然后else if (document.form_test.food.options[document.form_test.food.selectedIndex].value=="")ie报告有问题
导致脚本错误,所以不会执行这个函数

最后:
爱吃的食物:<select size="1" name="food">
<option>香蕉</option>
<option>苹果</option>
</select>
food中肯定有一个被选中了,默认是第一个(香蕉),因为它显示出来了呀。
woshihuzi 2005-04-27
  • 打赏
  • 举报
回复
呵呵,你真厉害,Accelerator(),马上结贴。
谢谢你!!
Accelerator 2005-04-27
  • 打赏
  • 举报
回复
1.随便用哪个都可以,我是加了这个忘了删那个了
2.java,c,c++通常一句代码后面就要跟分号,习惯问题
3.那是因为当函数代码有错误时,不会被执行(或者执行的时候忽略)
woshihuzi 2005-04-27
  • 打赏
  • 举报
回复
Accelerator()老兄,你的代码真灵。谢谢!不过我有几点疑问,还请你或者其他高手帮忙解答一下:
====================================================
第一、
<form method="POST" name="form_test" action=2.asp onsubmit="return chk_form()">
当中的
onsubmit="return chk_form()"

<input type="submit" value="提交" name="B1" onclick="return chk_form();">
当中的
onclick="return chk_form();"
两者留任何一个就能实现全部需要的功能。上面的代码把这两部分都留着,有没有什么特别的含义?

第二、
onsubmit="return chk_form()"
onclick="return chk_form();"
这两段一个带分号,一个不带分号,这有什么区别?我试了一下,它们带不带分号都没有问题。写成这样仅仅是程序员的习惯,还是别有深意?

第三、
按照Accelerator()老兄的分析:
if (document.form_test.yourname.value=="")
alert("姓名不能为空。");
return false;
差括号,如上的话函数永远返回false
可是我故意不输入姓名提交,验证函数必定返回false为什么还能够提交表单呢?
woshihuzi 2005-04-26
  • 打赏
  • 举报
回复
高手们,难道不屑于回答这个问题么?
woshihuzi 2005-04-26
  • 打赏
  • 举报
回复
按照 cqhunter(跑) 老兄的建议,把提交页面修改如下,结果输入“张三”,选了“苹果”,提交成功了。但是姓名文本框不输入任何东西的时候,也能成功提交。可以说,那个函数根本就没怎么调用。这违背了检查的初衷阿。我是学VBscript的,对lavascript不太了解。这样的问题希望各位高手明示啊:
======================================================
<html>
<script language="Javascript">
<!--
function chk_form() {
if (document.form_test.yourname.value=="")
alert("姓名不能为空。");
return false;
else if (document.form_test.food.options[document.form_test.food.selectedIndex].value=="")
alert("爱吃的食物不能为空。");
return false;
else
{
return true;
}
}
-->
</script>
<body>
<form method="POST" name="form_test" action=2.asp onsubmit="return chk_form()">
姓名:<input type="text" name="yourname"><br>
爱吃的食物:<select size="1" name="food">
<option>香蕉</option>
<option>苹果</option>
</select><br>
<input type="submit" value="提交" name="B1">
</form>
</body>
</html>
woshihuzi 2005-04-26
  • 打赏
  • 举报
回复
我的localhost网站一直是默认使用VBscript脚本语言。
这里用Javascript,还用在网站上作什么特别设置吗?
cqhunter 2005-04-26
  • 打赏
  • 举报
回复
<form method="POST" name="form_test" action=2.asp onsubmit="return chk_form()">
chk_form事件好像应该在这里提交
function chk_form() {
if (document.form_test.yourname.value=="")
alert("姓名不能为空。");
return false;
else if (document.form_test.food.options[document.form_test.food.selectedIndex].value=="")
alert("爱吃的食物不能为空。");
return false;
else
{
return true;
}
}


woshihuzi 2005-04-26
  • 打赏
  • 举报
回复
对高手来说,代码比较简单,哪位能修改一下,把运行通过的代码贴在这上面呢?
我一直用VBscript,不会Javascript。所以很头疼。
如果有高手告诉我用VBscript如何检测有关表单元素的内容,我就不用Javascript了。
woshihuzi 2005-04-26
  • 打赏
  • 举报
回复
我改成这样,还是不行啊。输入“张三”,选了“苹果”,还是不让提交啊。
========================================================
function chk_form() {
if (document.form_test.yourname.value=="")
alert("姓名不能为空。");
else if (document.form_test.food.options[document.form_test.food.selectedIndex].value=="")
alert("爱吃的食物不能为空。");
else
{
document.form_test.submit();
}
}
menrock 2005-04-26
  • 打赏
  • 举报
回复
document.form_test.food.options[document.form_test.food.selectedIndex].value

28,406

社区成员

发帖
与我相关
我的任务
社区描述
ASP即Active Server Pages,是Microsoft公司开发的服务器端脚本环境。
社区管理员
  • ASP
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧