Safari 5.1.7在JSP中复制文本区域中的文本

时间:2014-12-12 13:58:47

标签: javascript jsp safari textarea

在我的项目中,我在JSP上从网页中检索拒绝备注。

        <%
        String rejectRemarks = null;
        String rejectType = null; 
        String isScrap = "N";
        if(request.getParameter("rejectRemarks") != null && request.getParameter("rejectRemarks")!="undefined" )
        {
            rejectRemarks = request.getParameter("rejectRemarks");
            rejectType    = request.getParameter("rejectType");
            //System.out.println("in remarks.jsp reject remarks before substring ::" + rejectRemarks);
            if(rejectType != null && rejectType.equals("S"))
                isScrap="Y";            
        }   
        else    
            rejectRemarks = "";

        %>  
        <html>
        <head>
        <script  type="text/javascript">
        var remarksValue = "";
        function showDetails()
        {
        var maxlength=255;
        var e = window.event;
        remarksValue = document.getElementById("remarks").value;
        if(remarksValue == undefined || remarksValue == null ||MyTrim(remarksValue).length==0)
        {
                remarksValue = "";      
                document.getElementById("validity").value="Enter valid remarks";
                document.getElementById("remarks").value="";
        }
        else
        {
            if(remarksValue.length>maxlength) 
            {

                showError("TCW325","","","",true);
                return;     
            }
        window.returnValue=remarksValue;
        document.getElementById("remarksEntered").value = remarksValue;
        window.close();
        }
        }
        function fCase(o)
        {
        o.value=o.value.toUpperCase();
        }
        function wc()
        {
            var bwserAgnt = navigator.userAgent;
            if(bwserAgnt.indexOf("Chrome")!= -1)
            {
                document.onkeydown =  function(e)
                { 
                    if(remarksValue.length>255 && e.keyCode!=8) 
                     {
                         var remarksValue = document.getElementById("remarks").value;
                         remarksValue = remarksValue.slice(0,-1);
                         document.getElementById("remarks").value = remarksValue;  
                     }
                     else
                     wcNew(e);
                }
            }
            else if(bwserAgnt.indexOf("Safari")!= -1)
            {
                document.onkeydown =  function(event)
                { 
                    event = event || window.event;
                    if (event.keyCode == 8 || event.keyCode == 37 ||
                        event.keyCode == 38 || event.keyCode == 39 ||
                        event.keyCode == 46 || event.keyCode == 9 || event.keyCode == 37 || event.keyCode == 16) 
                        {
                            if(remarksValue.length>255 && e.keyCode!=8) 
                            {
                                var remarksValue = document.getElementById("remarks").value;
                                remarksValue = remarksValue.slice(0,-1);
                                document.getElementById("remarks").value = remarksValue;
                            }
                            else
                            wcNew(event);
                        return true;
                    }
                    else 
                    {
                                 return false;
                    }
                }
            }
            else if(bwserAgnt.indexOf("Firefox")!= -1)
            {
                var e = window.event;
                 if(window.event.keyCode!=8) 
                 {
                    var remarksValue = document.getElementById("remarks").value;
                 if(remarksValue.length>255){
                    remarksValue = remarksValue.slice(0,-1);
                 }
                    document.getElementById("remarks").value = remarksValue;  
                 }
                 wcNew(e);
            }
            else
            {
                var e = window.event;
                 if(window.event.keyCode!=8) 
                 {
                    var remarksValue = document.getElementById("remarks").value;
                 if(remarksValue.length>255){
                    remarksValue = remarksValue.slice(0,-1);
                 }
                    document.getElementById("remarks").value = remarksValue;  
                 }
                 wcNew(e);
            }
         } 
        function wcNew(e)
        { 
            characterCode = e.keyCode ;//character code is contained in IE's keyCode property
            if(characterCode == 13)
            {
                var remarksValue = document.getElementById("remarks").value;
                document.getElementById("remarksEntered").value = remarksValue;
                showDetails();
            }
        }
        function checkMaxLength(me)
        {
        var maxlength=255;
        var strRemarks=me.value;
        if(strRemarks.length >= maxlength )
        {
            alert("Remarks should not exceed 255 characters");
            return false;
        }
        }
        function close1()
        {
        window.close();
        }
        </script>
        <%
        if(isScrap.equals("Y")){
        %>
        <title>Scrap Remarks</title>
        <%
        }else
        {
        %>
        <title>Reject Remarks</title>
        <%}%>
        </head>

        <body onKeyPress="wc();" >
        <form  Autocomplete="off"  name ="frmMain" id="frmMain" method="post" >
        <table>
        <tr>
        <td>
        <INPUT TYPE="text" name="validity" id="validity" CLASS='cwInputBoxReadOnly' readOnly >
        </td>
        </tr>

        <tr>
        <td>
            <textarea id="remarks" name="remarks" rows="8" cols="25" onkeypress="return checkMaxLength(this);" ><%=rejectRemarks==null?"":rejectRemarks%></textarea>
        </td>
        </tr>
        </table>
        <table align="center">
        <tr>    
        <td>
        <input type="button" class="grayTabHeaderSelected" id ="OK" name="OK" value="OK"  align="CENTER" onClick="javascript:showDetails()"/>
        </td>
        <td>
        <input type="button" class="grayTabHeaderSelected" id ="CANCEL" name="CANCEL" value="CANCEL"  align="CENTER" onClick="close1()"/>
            </td>
        </tr>
        </table>
        <input type="hidden" id="remarksEntered" name="remarksEntered">


        </body>
        </html>

在那个方法中,这个方法WC();在页面上调用了按键。 它检查所有浏览器的代码。我在safari浏览器上遇到的问题是在文本框中输入的任何键。它出现两次,例如hhii mmyy nnaammee iiss SSaameerr。 在谷歌搜索了很多,并根据这个实现了代码。我按照下面的链接 http://answers.microsoft.com/en-us/windows/forum/windows_7-networking/characters-entered-twice-in-modal-dialog-under/134789c1-7845-4d10-ade2-9ff3b3a60085

  

我仍然面临着这个问题,因为第一个角色会重复,例如SSameer。只有当该JSP发生新的点击时才会发生这种情况。

任何帮助apprciated。 提前谢谢。

1 个答案:

答案 0 :(得分:0)

我找到了一个解决方案。我在同一个方法WC()中添加了keypress事件的代码。 key =全局变量

document.onkeypress =  function(event)
{
    if(key == event.keyCode)
    {
        return false;
    }
    else
    {
        key = event.keyCode;
    }
}

它对我有用。