问题描述:选中改行,改行颜色变灰。选中后点击修改按钮就会提交。。
但是我不选中的话,点击查修改按钮也会提交。我就是想,如果没选中的话,就不执行提交操作<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('./project.do?method=queryindex','_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><input type="button" value="修改" style="width:50px; height:40px;" onclick="javascript:window.open(reurl3(),'_self')"/>
因为打开的是一个页面,所以没选中的话 也会提交。
我现在就是想控制不提交。

解决方案 »

  1.   

    <script type="text/javascript">
    function dost(){
    var url = reurl3();
    if(flag ==false){
       return;
    }else{
      window.open(url,'_self');
    }
    }</script><input type="button" value="修改" style="width:50px; height:40px;" onclick="javascript:dost()"/>试改成这样。
      

  2.   

    问题描述:选中改行,改行颜色变灰。
    能变灰啊,哪有问题?<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:return edit();" />在JavaScript中添加edit()方法,代码如下function edit(){
         if(selectTable()){
         window.open(reurl3(),'_self')
         }else{
         return false;
         }
        }并且把selectTable()方法添加【返回值】,具体如下 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;
          return false;// 添加返回值
        }
          return true;// 添加返回值
         
      }我测试了,没问题。
      

  3.   

    如果使用FORM标签的话,可以设定里面的onsubmit="return function(){…………}"在里面加上查看currentActiveRow是否存在的代码就是了,如果不存在返回false