关于submit提交input不能为空的问题~

快跑蜗牛哥 2008-04-25 02:48:51
如下代码:有3个input 对应3个submit
现在要求当input 中,name的值相同时,
如;name='textfield'已经固定,不能更改,
能否实现,当点击submit时,
只要对应的input的值为空就弹出警告
请问这样的要求能否实现,
当然,有更多对应的input与submit也要能实现
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>1</title>
<script>
function myclick()
{
if(document.all("textfield").value == "")
{
alert("不能为空!");
document.all("textfield").focus();
return false;
}
else
{return true;}

}
</script>
</head>
<body>
<form name="form1" method="post" action="xx.jsp">
<label>
<input type="text" name="textfield" id="t">
<input type="submit" name="Submit" value="提交" onClick="return myclick()"><BR>
<input type="text" name="textfield" id="t">
<input type="submit" name="Submit" value="提交" onClick="return myclick()"><BR>
<input type="text" name="textfield" id="t">
<input type="submit" name="Submit" value="提交" onClick="return myclick()">
</label>
</form>
</body>
</html>
...全文
828 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
ahua3515 2008-04-25
  • 打赏
  • 举报
回复
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>1 </title>
<script>
function myclick(objname)
{
if(document.all(objname)[0].value == "")
{
alert("不能为空!");
document.all(objname)[0].focus();
}
else
{
document.form1.submit();
}

}
</script>
</head>
<body>
<form name="form1" method="post" action="xx.jsp">
<label>
<input type="text" name="t1" id="t">
<input type="button" name="t1" value="提交" onClick=" myclick(this.name)"> <BR>
<input type="text" name="t2" id="t">
<input type="button" name="t2" value="提交" onClick=" myclick(this.name)"> <BR>
<input type="text" name="t3" id="t">
<input type="button" name="t3" value="提交" onClick=" myclick(this.name)">
</label>
</form>
</body>
</html>

这样就可以实现你的想法了
当然我现在把button的name对应的text的name搞成以样了。你也可以搞成不一样,那样,你就在myclick(this.name)的this.name改成对应text的name就可以了
kingdomofhavean 2008-04-25
  • 打赏
  • 举报
回复
<form name="form1" method="post" action="xx.jsp">
<input type="text" name="textfield" id="t">
<input type="submit" name="Submit" value="提交" onClick="return myclick()"> <BR>
</form>

<form name="form2" method="post" action="xx.jsp">
<input type="text" name="textfield" id="t">
<input type="submit" name="Submit" value="提交" onClick="return myclick()"> <BR>
</form>

<form name="form3" method="post" action="xx.jsp">
<input type="text" name="textfield" id="t">
<input type="submit" name="Submit" value="提交" onClick="return myclick()"> <BR>
</form>
hyxd2 2008-04-25
  • 打赏
  • 举报
回复
LZ这么做的目的是什么啊?需求说清楚,你这个页面简直....
kingssq 2008-04-25
  • 打赏
  • 举报
回复
<html> 
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>1 </title>
<script>
function myclick(id)
{
if(document.getElementById(id).value == "")
{
alert("不能为空!");
document.getElementById(id).focus();
return false;
}
else
{
return true;}

}
</script>
</head>
<body>
<form name="form1" method="post" action="xx.jsp">
<label>
<input type="text" name="textfield" id="t">
<input type="button" name="ck" value="提交" onClick="return myclick('t')"> <BR>
<input type="text" name="textfield" id="t1">
<input type="button" name="ck" value="提交" onClick="return myclick('t1')"> <BR>
<input type="text" name="tecxtfield" id="t2">
<input type="button" name="ck" value="提交" onClick="return myclick('t2')">
</label>
</form>
</body>
</html>

关于处理更多:
如果你的控件是放在循环里面,你可以设置一个变量i,<%int i=0;%>
然后把textfield的id设置成和i相关,如:<input type="text" name="textfield" id="t<%=i%>">
<input type="button" name="ck" value="提交" onClick="return myclick('t<%=i%>')">
6216 2008-04-25
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 hellwindy 的回复:]
type="submit"的会无视onClick,改成type="button",并且按钮都不写name=submit

不然就算你只写一个也不能用,存在名字为"submit"的function和input会冲突
[/Quote]
maxel 2008-04-25
  • 打赏
  • 举报
回复
搞不懂,三个input框为啥用同名的呢?你怎么知道取的哪个值?
把input换成button好了,分别写三个函数验证输入情况
fcoolx 2008-04-25
  • 打赏
  • 举报
回复
name的值相同
id 让它不一样
然后根据id判断是哪个??

onClick="return myclick(对应id为参数)">
全粘架构师 2008-04-25
  • 打赏
  • 举报
回复
type="submit"的会无视onClick,改成type="button",并且按钮都不写name=submit

不然就算你只写一个也不能用,存在名字为"submit"的function和input会冲突
fuyou001 2008-04-25
  • 打赏
  • 举报
回复
关注下......
s_liangchao1s 2008-04-25
  • 打赏
  • 举报
回复
3个提交按钮 有什么用途阿??

81,091

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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