<div id="workshowDiv">
<form action="#" method="post"  id="workreport"
onsubmit="new Ajax.Updater({success:'workshowDiv',failure:'rightWorkSpace'}, '#springUrl("${HuiYee.spaceContextUri}")emr/report/generalx.html',{asynchronous:true, parameters:Form.serialize(this)});return false;">

<table width="100%" cellpadding="0" border="0">
<tr>
<td align="center">
起始日期:&nbsp;&nbsp;
<input class="input_txt" type="text" id="startDate" name="sdate" value="#if($queryObj.startDate)$!date.format('yyyy-MM-dd',$!{queryObj.startDate})#end" onfocus="showCalendar('',this,this,'$!{in}','holder',0,10,1)" msg="日期格式不正确" dataType="Date" require="false">
&nbsp;&nbsp;截止至&nbsp;&nbsp;
<input class="input_txt" type="text" id="endDate" name="edate" value="#if($queryObj.endDate)$!date.format('yyyy-MM-dd',$!{queryObj.endDate})#end" onfocus="showCalendar('',this,this,'$!{in}','holder',0,10,1)"  msg="日期格式不正确" dataType="Date" require="false">
</td>
</tr>
<tr><td></td></tr>
<tr>
<td colspan="2" align="center">
    <input type="submit"  onclick="alert(document.getElementById('startDate').value)"  value=" 查 询 " class="huiyeeBtn"/> &nbsp;&nbsp;&nbsp;
    #set ($myowner = $!HuiYee.getBusiness().id)
    #if ($myowner)
<input type="button" class="huiyeeBtn" value=" 报 表 " onClick="params=encodeURIComponent(Form.serialize(workreport)+'&owner='+'$myowner');new Ajax.Updater('reporteditView', '#springUrl("${HuiYee.spaceContextUri}reportdownload/report/edt.html?handler=ERN&params=")'+params,{method:'get',asynchronous:true,evalScripts:true});effect_1 = showForm(event,'reporteditView',[]);return false;">
#end
</td>
</tr>
</table>
</form>
要求加个onclick事件判断两个时间不超过3个月.超过不予提交到后台,并给个alt提示下..对js实在是很陌生..求教大神们给想想办法啊....

解决方案 »

  1.   

    <script>
    function getDateFromElement(str){
    var arr = document.getElementById(str).value.match(/\d+/g);
    return new Date(arr[0],arr[1],arr[2]);
    }
    function comp(){
    var ds=getDateFromElement("sd");
    var es=getDateFromElement("ed"); if ((es-ds)/86400000<90)
    {
    alert("日期少于90天");
    return false;
    }else{
    return true;
    }
    }</script>
    <form onsubmit="return comp()">
    <input value="2011-7-3" type="text" id="sd"/>
    <input value="2011-9-20" type="text" id="ed"/>
    <input type=submit value="比较"/>
    </form><script>
    function getDateFromElement(str){
    var arr = document.getElementById(str).value.match(/\d+/g);
    return new Date(arr[0],arr[1],arr[2]);
    }
    function comp(){
    var ds=getDateFromElement("sd");
    var es=getDateFromElement("ed"); if ((es-ds)/86400000<90)
    {
    alert("日期少于90天");
    return false;
    }else{
    return true;
    }
    }</script>
    <form onsubmit="return comp()">
    <input value="2011-7-3" type="text" id="sd"/>
    <input value="2011-10-20" type="text" id="ed"/>
    <input type=submit value="比较"/>
    </form>
      

  2.   

    try:
    改onsubmit事件过程为: var d1=Date(document.workreport.startDate.value);
    var d2=Date(document.workreport.endDate.value);
    if((d2.getTime()-d1.getTime())/1000/60/60/24>90){
    return false;
    }else{
    new Ajax.Updater({success:'workshowDiv',failure:'rightWorkSpace'}, '#springUrl("${HuiYee.spaceContextUri}")emr/report/generalx.html',{asynchronous:true, parameters:Form.serialize(this)});return false;
    }
    未加引号,建议另建一个函数,由onsubmit调用.
      

  3.   

    能不能用onclick事件判断呢?用了楼上大哥们的方法好像不起作用啊
      

  4.   

     onclick是做不到阻止表单提交的,除非你的表单是在onclick事件里面提交。
      

  5.   

    <script type="text/javascript"> selectgrp=function(){
     var d1=Date(document.workreport.startDate.value);
        var d2=Date(document.workreport.endDate.value);
        if((d2.getTime()-d1.getTime())/1000/60/60/24>90){
            alert("日期应少于90天");
            return false;
        }else{
            new Ajax.Updater({success:'workshowDiv',failure:'rightWorkSpace'}, '#springUrl("${HuiYee.spaceContextUri}")emr/report/generalx.html',{asynchronous:true, parameters:Form.serialize(this)});return false;
        }
    }
    </script>


    <div id="workshowDiv">
    <form action="#" method="post"  id="workreport"   onsubmit="selectgrp()" >

    <table width="100%" cellpadding="0" border="0">
    <tr>
    <td align="center">
    起始日期:&nbsp;&nbsp;
    <input class="input_txt" type="text" id="startDate" name="sdate" value="#if($queryObj.startDate)$!date.format('yyyy-MM-dd',$!{queryObj.startDate})#end" onfocus="showCalendar('',this,this,'$!{in}','holder',0,10,1)" msg="日期格式不正确" dataType="Date" require="false">
    &nbsp;&nbsp;截止至&nbsp;&nbsp;
    <input class="input_txt" type="text" id="endDate" name="edate" value="#if($queryObj.endDate)$!date.format('yyyy-MM-dd',$!{queryObj.endDate})#end" onfocus="showCalendar('',this,this,'$!{in}','holder',0,10,1)"  msg="日期格式不正确" dataType="Date" require="false">
    </td>
    </tr>
    <tr><td></td></tr>
    <tr>
    <td colspan="2" align="center">*查询日期跨度在3个月内
        <input type="submit"    value=" 查 询 " class="huiyeeBtn"/> &nbsp;&nbsp;&nbsp;
        #set ($myowner = $!HuiYee.getBusiness().id)
        #if ($myowner)
    <input type="button" class="huiyeeBtn" value=" 报 表 " onClick="params=encodeURIComponent(Form.serialize(workreport)+'&owner='+'$myowner');new Ajax.Updater('reporteditView', '#springUrl("${HuiYee.spaceContextUri}reportdownload/report/edt.html?handler=ERN&params=")'+params,{method:'get',asynchronous:true,evalScripts:true});effect_1 = showForm(event,'reporteditView',[]);return false;">
    #end
    </td>
    </tr>
    </table>
    </form>
    <div id="BasicDataBody">
    这个是我改的代码..onsubmit时间好像就是调不到那个方法..