我要选中了改行,该行变成灰色,在路径里面才会执行提交。
 但是我如果没有选中,哪行。也会执行提交,这样就会报错了。
请各位帮忙下,如果没有选中行,在路径里面就中断。
大概意思就是这样,求大虾帮忙。
<script type="text/javascript">
  var currentActiveRow;   
  function changeActiveRow(obj)   
  {   
  if(currentActiveRow)   
    currentActiveRow.style.backgroundColor="";   
    currentActiveRow=obj;   
    currentActiveRow.style.backgroundColor="#dddddd";   
  }   
  
    var key = 0;
    var flag = false;
    var b='a';
 function selectTable() {
  var table = document.getElementById("table");
  var rows = table.rows.length;
 
  for(var i=0;i<rows;i++) {
  var row = table.rows[i];
  if(row.style.backgroundColor == "#dddddd") {
    key = row.cells[0].innerHTML; 
     flag = true;
    break ;
  }
  }
  if(!table.rows[i]){
  alert("请选择您要操作的记录!");
  flag = false;
    }
     
  }
 
   function test(){
   selectTable();
    if(flag == false) {
      return;
    } else {
    
     //alert('选中的行为:' + key);
     return key;
     
     }

}







function reurl()
{
     var a=test();
 var b='./project.do?method=delete&billno='+a;
    // alert(b);
    if(a=undefined)
     {
     return ;
     }
      return b;
     
}

<input type="button" value="删除" style="width:50px; height:40px;" onclick="javascript:window.open(reurl(),'_self')"/>

解决方案 »

  1.   

    onclick="javascript:window.open(reurl(),'_self')"/>
    这个里面会提交。
      

  2.   

    html
      

  3.   

    <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
    <%@ taglib uri="http://fenye.com" prefix="q"%>
    <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>湖南建工目标成本管理系统</title>
    <style type="text/css">
    body{text-align:center; font-size:14px; margin:0 auto; width:1024px;height:768px;  }
    #mainFrame{height:768px; width:1024px; margin:0 auto;padding:0px;}
     #head{height:120px;width:1024px;border:#666 1px solid;}

     #rightFrame{ height:648px; width:1024px;  margin: 0px;padding:0px; float:left;border:#666 1px solid; }
    .clearfloat { /* 此类应当放在 div 或 break 元素上,而且该元素应当是完全包含浮动的容器关闭之前的最后一个元素 */
    clear:both;
        height:0;
        font-size: 1px;
        line-height: 0px;
    }
    </style><script type="text/javascript">
      var currentActiveRow;   
      function changeActiveRow(obj)   
      {   
      if(currentActiveRow)   
        currentActiveRow.style.backgroundColor="";   
        currentActiveRow=obj;   
        currentActiveRow.style.backgroundColor="#dddddd";   
      }   
      
        var key = 0;
        var flag = false;
        var b='a';
     function selectTable() {
      var table = document.getElementById("table");
      var rows = table.rows.length;
     
      for(var i=0;i<rows;i++) {
      var row = table.rows[i];
      if(row.style.backgroundColor == "#dddddd") {
        key = row.cells[0].innerHTML; 
         flag = true;
        break ;
      }
      }
      if(!table.rows[i]){
      alert("请选择您要操作的记录!");
      flag = false;
        }
         
      }
     
       function test(){
       selectTable();
        if(flag == false) {
          return;
        } else {
        
         //alert('选中的行为:' + key);
         return key;
         
         }

    }







    function reurl()
    {
         var a=test();
     var b='./project.do?method=delete&billno='+a;
        // alert(b);
        if(a=undefined)
         {
         return ;
         }
          return b;
         
    }


    function reurl2()
    {
      var c=test();
      var d='./project.do?method=findkey&billno='+c;
      if(c=undefined)
      {
      return ;
      }
      return d;

    }


    function reurl3()
    {
      var e=test();
      var f="./project.do?method=xiugaiyemian&billno="+e;
      if(e=undefined)
      {
        return ;
      }
      return f;
      
    }   </script></head><body><!--最外层框架 -->
        <div id="mainFrame">
            
            <!--头部框架 -->
         <div id="head">
          <table border="0" cellpadding="0" cellspacing="0" width="100%" height="100%">
            <tr>
             <td width="13%" height="18">
                
                </td>
                <td width="60%">
                
            </td>
                <td width="27%">
                
                </td>
            </tr>
            <tr>
             <td height="101" align="center">
                 <img src="WebRoot/images/logo.jpg"/>
                </td>
                <td align="left">
                 <font color="#000000" style="font-size:48px" face="华文彩云">湖南建工目标成本管理系统</font>
                </td>
                <td>
                
                </td>
            </tr>
            <tr>
             <td>
                
                </td>
                <td>
                
                </td>
                <td>
                
                </td>
            </tr>
           </table>        
         </div><!--头部框架结束 -->
            <!--右部框架 -->
            <div id="rightFrame">
              <div style=" width:100%; height:70px;">
             <div style="margin-left:25px; height:40px; width:95%; margin-top:20px;">
                <table width="100%" height="100%" cellpadding="0" cellspacing="0" border="0">
                         <tr>
                             <td width="84%">
                                 <input type="button" value="查看" style="width:50px; height:40px;" onclick="javascript:window.open(reurl2(),'_self')"/>
                 <input type="button" value="新增" style="width:50px; height:40px;" onclick="javascript:window.open('./project.do?method=tianjiayemian','_self')"/>
                     <input type="button" value="修改" style="width:50px; height:40px;" onclick="javascript:window.open(reurl3(),'_self')"/>
                     <input type="button" value="删除" style="width:50px; height:40px;" onclick="javascript:window.open(reurl(),'_self')"/>
                                    <input type="button" value="查询" style="width:50px; height:40px;" onclick="javascript:window.open(reurl(),'_self')"/>
                                    <input type="button" value="核算科目设置" style="width:70px; height:40px;" onclick="javascript:window.open('...','_self')"/>
                     </td>
                                <td width="8%"></td>
                                <td width="8%">
                                 <input type="button" value="退出" style="width:50px; height:40px;"/>
                                </td>
                            </tr>
                        </table>
                </div>
            </div>
            <div style="width:100%; height:5px;"><hr/></div>
            
             <div
    style="width: 95%; height: 430px; font-size: 12px; overflow: auto; margin:0 auto;">
      <table width="100%" border="1" cellpadding="0" bordercolor="#666666"
    cellspacing="0" id="table">
    <tr class="scrollColThead">
    <th height="30" align="center" width="83">
    项目编码
                            </th>
    <th width="345" align="center" >
       项目名称
                            </th>
                            <th width="202" align="center" >
    经营单位
    </th>
                            <th width="131" align="center" >
    项目状态
                            </th>
                            <th width="199" align="center" >
    签订日期
                            </th>
    </tr>
                        
                        
                         <c:forEach items="${list}" var="l">   
           
           <tr onClick="changeActiveRow(this)">
    <td height="30">
    ${l.billno}
    </td>
    <td >
    ${l.name}
    </td>
                            <td>
    ${l.epsno}
    </td>
    <td>
    ${l.billstatus}
    </td>
                            <td>
    ${l.filldate}
    </td>
               
        </tr>  
       
        </c:forEach>  
                        
                        
                        


    </table>
    </div>
            </div>  <!--右部框架结束 -->
            <br class="clearfloat"/>
     
        </div><!--最外层框架结束 -->
     
    </body>
    </html>
      

  4.   

    function selectTable() {
          var table = document.getElementById("table");
          var rows = table.rows.length;
         
          for(var i=0;i<rows;i++) {
              var row = table.rows[i];
              if(row.style.backgroundColor == "#dddddd") {
                key = row.cells[0].innerHTML; 
                 flag = true;
                break ;
              }
          }
          if(!table.rows[i]){
          alert("请选择您要操作的记录!");
          flag = false;
        }
         
      }
    这个方法在你不选的情况下js报错了吗?
      

  5.   

    不报错 但是会弹出   alert("请选择您要操作的记录!");
    我的意思就是希望 如果没有选中的话,  弹出这个后,就终止。
     不在 onclick="javascript:window.open(reurl(),'_self')"/>
    里面提交打开窗口。
      

  6.   


    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head><meta http-equiv="Content-Type" content="text/html; charset=gbk" />
    <title>湖南建工目标成本管理系统</title>
    <style type="text/css">
        body{text-align:center; font-size:14px; margin:0 auto; width:1024px;height:768px;  }
        #mainFrame{height:768px; width:1024px; margin:0 auto;padding:0px;}
         #head{height:120px;width:1024px;border:#666 1px solid;}
        
         #rightFrame{ height:648px; width:1024px;  margin: 0px;padding:0px; float:left;border:#666 1px solid; }
        .clearfloat { /* 此类应当放在 div 或 break 元素上,而且该元素应当是完全包含浮动的容器关闭之前的最后一个元素 */
        clear:both;
        height:0;
        font-size: 1px;
        line-height: 0px;
    }
    </style><script type="text/javascript">
      var currentActiveRow;   
      function changeActiveRow(obj)   
      {   
      if(currentActiveRow)   
        currentActiveRow.style.backgroundColor="";   
        currentActiveRow=obj;   
        currentActiveRow.style.backgroundColor="#dddddd";   
      }   
      
        var key = 0;
        var flag = false;
        var b='a';
     function selectTable() {
          var table = document.getElementById("table");
          var rows = table.rows.length;
         
          for(var i=0;i<rows;i++) {
              var row = table.rows[i];
              if(row.style.backgroundColor) {
                key = +row.cells[0].innerHTML; 
                flag = true;
                break ;
              }
          }
          if(!table.rows[i]){
          alert("请选择您要操作的记录!");
          flag = false;    }
         
      }
     
       function test(){
           selectTable();
        if(flag == false) {
          return;
        } else {
        
        // alert('选中的行为:' + key);
         return key;
         }
        
        }
        
        
        
        
        
        
        
        function reurl()
        {
         var a=test();
     alert(a)
         var b='./project.do?method=delete&billno='+a;
        if(a=undefined)
         {
         return ;
         }
          return b;
         
        }
        
        
        function reurl2()
        {
          var c=test();
          var d='./project.do?method=findkey&billno='+c;
          if(c=undefined)
          {
          return ;
          }
          return d;
        
        }
        
        
        function reurl3()
        {
          var e=test();
          var f="./project.do?method=xiugaiyemian&billno="+e;
          if(e=undefined)
          {
            return ;
          }
          return f;
          
        }   </script></head><body><!--最外层框架 -->
        <div id="mainFrame">
            
            <!--头部框架 -->
            <div id="head">
                 <table border="0" cellpadding="0" cellspacing="0" width="100%" height="100%">
               <tr>
                <td width="13%" height="18">
                
                </td>
                <td width="60%">
                
            </td>
                <td width="27%">
                
                </td>
            </tr>
            <tr>
                <td height="101" align="center">
                    <img src="WebRoot/images/logo.jpg"/>
                </td>
                <td align="left">
                    <font color="#000000" style="font-size:48px" face="华文彩云">湖南建工目标成本管理系统</font>
                </td>
                <td>
                
                </td>
            </tr>
            <tr>
                <td>
                
                </td>
                <td>
                
                </td>
                <td>
                
                </td>
            </tr>
           </table>        
            </div><!--头部框架结束 -->
            <!--右部框架 -->
            <div id="rightFrame">
                 <div style=" width:100%; height:70px;">
                <div style="margin-left:25px; height:40px; width:95%; margin-top:20px;">
                <table width="100%" height="100%" cellpadding="0" cellspacing="0" border="0">
                            <tr>
                                <td width="84%">
                                    <input type="button" value="查看" style="width:50px; height:40px;" onclick="javascript:window.open(reurl2(),'_self')"/>
                                    <input type="button" value="新增" style="width:50px; height:40px;" onclick="javascript:window.open('./project.do?method=tianjiayemian','_self')"/>
                                    <input type="button" value="修改" style="width:50px; height:40px;" onclick="javascript:window.open(reurl3(),'_self')"/>
                                    <input type="button" value="删除" style="width:50px; height:40px;" onclick="javascript:window.open(reurl(),'_self')"/>
                                    <input type="button" value="查询" style="width:50px; height:40px;" onclick="javascript:window.open(reurl(),'_self')"/>
                                    <input type="button" value="核算科目设置" style="width:70px; height:40px;" onclick="javascript:window.open('...','_self')"/>
                                </td>
                                <td width="8%"></td>
                                <td width="8%">
                                    <input type="button" value="退出" style="width:50px; height:40px;"/>
                                </td>
                            </tr>
                        </table>
                </div>
            </div>
            <div style="width:100%; height:5px;"><hr/></div>
            
                <div
                    style="width: 95%; height: 430px; font-size: 12px; overflow: auto; margin:0 auto;">
                  <table width="100%" border="1" cellpadding="0" bordercolor="#666666"
                        cellspacing="0" id="table">
                        <tr class="scrollColThead">
                            <th height="30" align="center" width="83">
                                项目编码
                            </th>
                            <th width="345" align="center" >
                                  项目名称
                            </th>
                            <th width="202" align="center" >
                                经营单位
                            </th>
                            <th width="131" align="center" >
                                项目状态
                            </th>
                            <th width="199" align="center" >
                                签订日期
                            </th>
                        </tr>
                        
                        
                         <c:forEach items="${list}" var="l">   
           
           <tr onClick="changeActiveRow(this)">
                            <td height="30">
                                1
                            </td>
                            <td >
                                ${l.name}
                            </td>
                            <td>
                                ${l.epsno}    
                            </td>
                            <td>
                                ${l.billstatus}    
                            </td>
                            <td>
                                ${l.filldate}    
                            </td>
               
        </tr>        <tr onClick="changeActiveRow(this)">
                            <td height="30">
                                2  
                            </td>
                            <td >
                                ${l.name}
                            </td>
                            <td>
                                ${l.epsno}    
                            </td>
                            <td>
                                ${l.billstatus}    
                            </td>
                            <td>
                                ${l.filldate}    
                            </td>
               
        </tr>  
       
        </c:forEach>  
                        
                        
                        
                        
                                
                    </table>
                </div>
            </div>  <!--右部框架结束 -->
            <br class="clearfloat"/>
     
        </div><!--最外层框架结束 -->
     
    </body>
    </html>楼主 试试
      

  7.   

    function test(){
           selectTable();
        if(flag == false) {
          return;
        } else {
        
         //alert('选中的行为:' + key);
         return key;
         
         }
        
        }
    在if里面加alert();看看是否进入,我感觉应该是if(flag == false)的问题!
      

  8.   

          for(var i=0;i<rows;i++) {
              var row = table.rows[i];
              if(row.style.backgroundColor) {
                key = +row.cells[0].innerHTML; 
                flag = true;
                break ;
              }
          }
      

  9.   

    试试
    if("undefined" == a)
      

  10.   

    function reurl()
        {
         var a=test();
         alert(a)
         var b='./project.do?method=delete&billno='+a;
        if(a=undefined)
         {
         return ;
         }
          return b;
         
        }
    你打印出来的a是什么?还有就是一个=号是赋值的意思,你的竟然没报错?