onsubmit 的问题

Miracle_lucy 2013-07-20 06:03:53

<script src="js/1.js" type="text/javascript"></script>
。。。。省略部分代码
<form action="success.jsp" method="post" name="myform" onsubmit ="return checknews();" >
<table>
<tr>
<td> <input type="submit" value="提交" /></td>
<td ><input type="reset" value="重置"/></td>
</tr>
</table>
</form>

1.js
function checknews(){
var ntopic=document.getElementsByName("topic");
var ntitle=document.getElementsByName("title");
var nauthor=document.getElementsByName("author");
var nsummary=document.getElementsByName("summary");
var ncontent=document.getElementsByName("content");
if(ntopic==null||ntopic==""){
alter("请选择主题");
return false;
}
if(ntitle==null||ntitle==""){
alter("请输入标题");
return false;
}
if(nauthor==null||nauthor==""){
alter("请输入作者");
return false;
}
if(nsummary==null||nsummary==""){
alter("请输入摘要");
return false;
}
if(ncontent==null||ncontent==""){
alter("请选择内容");
return false;
}
}


为什么checknews() 点击提交就成功呢?帮忙提示下
...全文
190 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
Miracle_lucy 2013-07-21
  • 打赏
  • 举报
回复
谢谢大家,结贴啦。2楼的同胞,其实不用你这样的也可以,我的逻辑是正确的。各种小错误,丢三落四的,单词拼写错误,遗漏关键字等的。总之谢谢各位了
Miracle_lucy 2013-07-21
  • 打赏
  • 举报
回复
引用 10 楼 shy315 的回复:
.value
谢谢提醒。O(∩_∩)O哈哈~我自己有找出了一个真的很坑爹的错误。完全是单词不过关,alert
shy315 2013-07-21
  • 打赏
  • 举报
回复
.value
Miracle_lucy 2013-07-20
  • 打赏
  • 举报
回复
<script>
function checknews(){
	var ntopic=document.getElementById("topic");
	var ntitle=document.getElementById("title");
	var nauthor=document.getElementById("author");
	var nsummary=document.getElementById("summary");
	var ncontent=document.getElementById("content");
	if(ntopic==null||ntopic==""){
		alter("请选择主题");
		return false;
	}
	else if(ntitle==null||ntitle==""){
		alter("请输入标题");
		return false;
	}
	else if(nauthor==null||nauthor==""){
		alter("请输入作者");
		return false;
	}
	else if(nsummary==null||nsummary==""){
		alter("请输入摘要");
		return false;
	}
	else if(ncontent==null||ncontent==""){
		alter("请选择内容");
		return false;
	}
	else{
		return true;	
	}
	return false;
}



</script>
<body>

 <div style="background:url(Images/opt_name.gif) no-repeat; padding-left:10px; margin-bottom:20px; margin-top:30px;"> 添加新闻:</div>

<form action="success.jsp" method="post" name="myform" onsubmit ="return checknews()" >
   <table id="main" border="0" cellspacing="0" cellpadding="0" >
   <tr>
   <td >主题:</td>
   <td><select name="topic" id="topic">
    <option value="1">国内</option>
    <option value="2">国际</option> 
    <option value="3">军事</option> 
    <option value="4">体育</option> 
    <option value="5">娱乐</option> 
    <option value="6">社会</option>
    <option value="7">财经</option> 
    <option value="8">科技</option> 
    <option value="9">汽车</option> 
    <option value="10">教育</option>
    <option value="11">房产</option>   
    <option value="12">其他</option>   
    <option value="13">全部</option>             
    </select></td>           
   </tr>
    <tr>
    <td >标题:</td>
    <td> <input name="title" type="text" id="title"/></td>   
   </tr>
   <tr>
    <td >作者:</td>
    <td ><input name="author" type="text" id="author"/></td>   
   </tr>
   
   <tr>
    <td >摘要:</td>
    <td ><textarea cols="30" rows="4" name="summary"  id="summary" >
</textarea></td>   
   </tr>
   <tr>
    <td >内容:</td>
    <td ><textarea cols="50" rows="10" name="content" id="content">
</textarea></td>   
   </tr>
   
   <tr>
    <td> <input type="submit" value="提交"  /></td>
    <td ><input type="reset"  value="重置"/></td>   
   </tr>
  </table>
</form>
</body>
</html>
我把js文件嵌入进来了 ,大家看一下完整的代码。 如果把onsubmit ="return checknews()" 换成onsubmit ="return false" 点击提交不跳转 。到底是哪里,还是函数依然不对?检查不出来哪错了
shy315 2013-07-20
  • 打赏
  • 举报
回复
var ntopic=document.getElementById("topic"); if (ntopic && ntopic.value=="") { return false; } 先试试一个控件。
Miracle_lucy 2013-07-20
  • 打赏
  • 举报
回复
引用 6 楼 shy315 的回复:
var ntopic=document.getElementsByName("topic"); Elements是复数,表示返回列表。楼主要好好看看文档,看看示例。 另外文本框内容是value属性。 ntopic.length==0表示没有该名字的控件 判断内容用ntopic[0].value
多谢大虾指点,所言即是。与表单提交的小脚本弄混了。给对应的标签加了id,改过后var ntopic=document.getElementById("topic"); 仍然实现不了。继续求解,这是怎么了?究竟是有多坑爹的错误啊,总是找不出来
shy315 2013-07-20
  • 打赏
  • 举报
回复
var ntopic=document.getElementsByName("topic"); Elements是复数,表示返回列表。楼主要好好看看文档,看看示例。 另外文本框内容是value属性。 ntopic.length==0表示没有该名字的控件 判断内容用ntopic[0].value
Miracle_lucy 2013-07-20
  • 打赏
  • 举报
回复
引用 3 楼 wljk506 的回复:
2 楼 正确
没反应呀 大虾帮忙再提示下 不知道哪错了···
Miracle_lucy 2013-07-20
  • 打赏
  • 举报
回复
引用 2 楼 a470577391 的回复:
function checknews(){
	var ntopic=document.getElementsByName("topic");
	var ntitle=document.getElementsByName("title");
	var nauthor=document.getElementsByName("author");
	var nsummary=document.getElementsByName("summary");
	var ncontent=document.getElementsByName("content");
	if(ntopic==null||ntopic==""){
		alter("请选择主题");
		return false;
	}
	else if(ntitle==null||ntitle==""){
		alter("请输入标题");
		return false;
	}
	else if(nauthor==null||nauthor==""){
		alter("请输入作者");
		return false;
	}
	else if(nsummary==null||nsummary==""){
		alter("请输入摘要");
		return false;
	}
	else if(ncontent==null||ncontent==""){
		alter("请选择内容");
		return false;
	}
        else{
                return true;
        }
        return false;
}
还是没有反应 难道是非语法错误?
风.foxwho 2013-07-20
  • 打赏
  • 举报
回复
2 楼 正确
白开水MD5 2013-07-20
  • 打赏
  • 举报
回复
function checknews(){
	var ntopic=document.getElementsByName("topic");
	var ntitle=document.getElementsByName("title");
	var nauthor=document.getElementsByName("author");
	var nsummary=document.getElementsByName("summary");
	var ncontent=document.getElementsByName("content");
	if(ntopic==null||ntopic==""){
		alter("请选择主题");
		return false;
	}
	else if(ntitle==null||ntitle==""){
		alter("请输入标题");
		return false;
	}
	else if(nauthor==null||nauthor==""){
		alter("请输入作者");
		return false;
	}
	else if(nsummary==null||nsummary==""){
		alter("请输入摘要");
		return false;
	}
	else if(ncontent==null||ncontent==""){
		alter("请选择内容");
		return false;
	}
        else{
                return true;
        }
        return false;
}
Miracle_lucy 2013-07-20
  • 打赏
  • 举报
回复
checknews() 不起作用,页面直接跳转 这是怎么回事

81,092

社区成员

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

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