<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() 点击提交就成功呢?帮忙提示下

解决方案 »

  1.   

    checknews() 不起作用,页面直接跳转 这是怎么回事
      

  2.   

    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;
    }
      

  3.   

    var ntopic=document.getElementsByName("topic");
    Elements是复数,表示返回列表。楼主要好好看看文档,看看示例。
    另外文本框内容是value属性。
    ntopic.length==0表示没有该名字的控件
    判断内容用ntopic[0].value
      

  4.   

    多谢大虾指点,所言即是。与表单提交的小脚本弄混了。给对应的标签加了id,改过后var ntopic=document.getElementById("topic"); 仍然实现不了。继续求解,这是怎么了?究竟是有多坑爹的错误啊,总是找不出来
      

  5.   

    var ntopic=document.getElementById("topic");
    if (ntopic && ntopic.value=="") {
      return false;
    }
    先试试一个控件。
      

  6.   

    <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" 
    点击提交不跳转 。到底是哪里,还是函数依然不对?检查不出来哪错了
      

  7.   


    谢谢提醒。O(∩_∩)O哈哈~我自己有找出了一个真的很坑爹的错误。完全是单词不过关,alert