如何检测html文本字段是否处于活动状态?

时间:2014-11-25 00:00:52

标签: javascript forms jsp input

我正在调试旧的jsp / javascript页面,发生了一些奇怪的事情:页面上有一个帐单搜索表单,十几个输入和选择字段,它是一个struts应用程序,所有其他浏览器工作正常,除了IE 10,问题是当你第一次加载页面时,一切正常,但如果再次加载该页面,输入字段会冻结,当我鼠标悬停并单击字段时,没有任何反应,并且它不会让我输入信息,如果你第三次加载该页面,它再次正常,但在第四次,它被冻结,等等。有一个&#34; Clear&#34;页面上的按钮,当窗体被冻结时,您可以单击该按钮,它将调用javascript来清除缓存,它会将一些http会话设置为null并重定向到该页面,输入字段将再次处于活动状态。< / p>

以下是一些代码行:

    <form:form commandName="commandBean" 
     name="billingSearch">
      <div id="form-row">
      <div id="form-row-left">Carrier Name:</div>
      <div id="form-row-right">
      <form:select path="masterCompany" id="select_style">
      <form:options items="${commandBean.masterCompanyList}" itemValue="id" itemLabel="masterCompanyName"/>
      </form:select>
      </div>
      </div>

       <c:choose>
        <c:when test="${!commandBean.agent}">
            <div id="form-row">
            <div id="form-row-left">Agency Name:</div>
            <div id="form-row-right">
            <form:select path="agency" id="select_style">
                <form:options items="${commandBean.agencyList}" itemValue="id" itemLabel="agencyName"/>
            </form:select>
            </div>
            </div>
        </c:when>  
      </c:choose>
       <c:choose>
        <c:when test="${!commandBean.agent && !commandBean.mga && !commandBean.insured}">
            <div id="form-row">
            <div id="form-row-left">Agency Group:</div>
            <div id="form-row-right">
            <form:select path="agencyGroupId" id="select_style">
                <form:options items="${commandBean.agencyGroupList}" itemValue="id" itemLabel="groupName"/>
            </form:select>
            </div>
            </div>
        </c:when>  
      </c:choose>
    ...
      <div id="form-row">
      <div id="form-row-left">Posting Date:</div>
      <div id="form-row-right">
      <div id="form-row-date">
      <div id="date-start-label">Start:</div>
      <div id="date-start-box">
        <form:input path="startDate"  size="10"></form:input>
              <script type="text/javascript">
                   new tcal ({
                       'formname': 'commandBean',
                       'controlname': 'startDate'
                   });
              </script>
      </div>
      <div id="date-end-label">End:</div>
      <div id="date-end-box">
        <form:input path="endDate"   size="10" ></form:input>
              <script type="text/javascript">
                   new tcal ({
                       'formname': 'commandBean',
                       'controlname': 'endDate'
                   });
              </script>
      </div>
      </div>  
      </div>
      </div>
      <div id="form-submit-row">
      <div id="submit-left"><input name="submit" id="button_style" value="Search" type="submit" /></div>
      <div id="submit-right"><input onclick="clearBillingCache();"  name="reset" id="button_style" value="Clear" type="button" /></div>
      </div>
    </form:form>

...

function clearBillingCache(){
    window.location = "billingSearchClear.html"
}

...


        @RequestMapping(value = "/billingSearchClear.html", method = RequestMethod.GET)
        public String clearCache(HttpServletRequest request) {
            String returnVal = "redirect:/billingSearch.html";


            request.getSession().setAttribute("stickyCarrier", null);
            request.getSession().setAttribute("stickyAgency", null);
            request.getSession().setAttribute("stickyGroup", null);
    ...
            request.getSession().setAttribute("stickyStartDate", null);
            request.getSession().setAttribute("stickyEndDate", null);

            return returnVal;
        }

我尝试过将以下内容添加到jsp页面,因此即使点击次数也会调用onload:

<%!
  int Counter=0;
%>

...     &LT;%     if(++ Counter%2!= 0)     {

%>
 <body onload="clearBillingCache()">
 <%
}
else
{

%>
<body>
<%
}
System.out.println("  Counter = "+Counter);
%>

即使点击也会刷新,但字段仍然被冻结,直到我点击&#34;清除&#34;按钮。

我打印出会话值,它们都是空的。

之前有没有遇到过这个问题?如何让它每次都正确加载表单而不点击清晰表格?

1 个答案:

答案 0 :(得分:0)

通过以下更改解决了问题:

function delay(ms) {
    var cur_d = new Date();
    var cur_ticks = cur_d.getTime();
    var ms_passed = 0;
    while(ms_passed < ms) {
        var d = new Date();  // Possible memory leak?
        var ticks = d.getTime();
        ms_passed = ticks - cur_ticks;
        d = null;            // Prevent memory leak?
    }
}

<body onload="delay(300)">