<script language="JavaScript" type="text/JavaScript">
function CheckNum()
{
if (window.event.keyCode==39)
{
  window.event.keyCode=0;
}
}
</script>

解决方案 »

  1.   

    <input name="abc" type="text" size="4" onKeyPress="CheckNum()">
      

  2.   

    TO JK_10000(JK):
    我的页面中有一个输入框<input type=text name=title size=46 maxlength=50 value=<%=title%>>我的目的是将数据库中的title字段的内容获取出来显示在输入框内,如果按照上面value的写法,那么如果title内容之中有空格,比如title="a  b  c",那么生成的页面value=a,不合要求;
    如果写成value='<%=title%>',那么如果用户输入的title中有引号,则会在第一个引号处截断,也不合要求,请教高手有何方法能够解决。谢谢
      

  3.   

    如果是jsp:数据输出时需要经过转化,例如显示为input value=""的内容,可以经过以下类里htmlEncoder的方法转化。
    <input type=text name=title size=46 maxlength=50 value="<%=title%>" >
    --->>>
    <input type=text name=title size=46 maxlength=50 value="<%=CommStr.htmlEncoder(title)%>" >
    /*
     * Created by Seamus
     * Updated on 2004-10-21 by JK
     *
     */
    package com.jk.util;public class  CommStr
    {
      /**
      *replace the old string to new string in the given destination string.
      */
      public static java.lang.String strReplace(java.lang.String destStr, java.lang.String oldStr, java.lang.String newStr)
      {
        if(destStr==null)
          return "";
        String tmpStr = destStr;
        int foundPos = tmpStr.indexOf(oldStr);
        while (foundPos>=0)
        {
          tmpStr = tmpStr.substring(0,foundPos) + newStr + tmpStr.substring(foundPos + oldStr.length(),tmpStr.length());
          foundPos = tmpStr.indexOf(oldStr,foundPos+newStr.length());
        }
        return tmpStr;
      }  /**
      *Encode for HTML.
      */
      public static String htmlEncoder(String str)
      {
        if(str==null || str.equals(""))
          return "";
        String res_str;
        res_str=strReplace(str,"<","&lt;");
        res_str=strReplace(str,">","&rt;");
        res_str=strReplace(str,"\"","&quot;");
        res_str=strReplace(str,"'","&#039;");
        return res_str;
      }  /**
      *Encode for HTML-Text.
      */
      public static String htmlTextEncoder(String str)
      {
        if(str==null || str.equals(""))
          return "";
        String res_str;
        res_str=strReplace(str,"<","&lt;");
        res_str=strReplace(str,">","&rt;");
        res_str=strReplace(str,"\"","&quot;");
        res_str=strReplace(str,"'","&#039;");
        res_str=strReplace(str," ","&nbsp;");
        res_str=strReplace(str,"\r\n","<br>");
        res_str=strReplace(str,"\r","<br>");
        res_str=strReplace(str,"\n","<br>");
        return res_str;
      }  /**
      *Encode for URL.
      */
      public static String urlEncoder(String str) {
        return java.net.URLEncoder.encode(str) ;
      }  /**
      *Encode for XML.
      */
      public static String xmlEncoder(String str)
      {
        if(str==null || str.equals(""))
          return "";
        String res_str;
        res_str=strReplace(str,"&","&amp;");
        res_str=strReplace(res_str,"<","&lt;");
        res_str=strReplace(res_str,">","&gt;");
        res_str=strReplace(res_str,"\"", "&quot;");
        res_str=strReplace(res_str,"\'", "&acute;");
        return res_str;
      }  /**
      *Encode for SQL.
      */
      public static String sqlEncoder(String str)
      {
        if(str==null || str.equals(""))
          return "";
        String res_str;
        res_str=strReplace(str,"'","''");
        return res_str;
      }  /**
      *Encode for Javascript.
      */
      public static String jsEncoder(String str)
      {
        if(str==null || str.equals(""))
          return "";
        String res_str;
        res_str=strReplace(str,"\\","\\\\");
        res_str=strReplace(str,"'","\\'");
        res_str=strReplace(str,"\"","\\\"");
        res_str=strReplace(str,"\r\n","\\\n");
        res_str=strReplace(str,"\n","\\\n");
        res_str=strReplace(str,"\r","\\\n");
        return res_str;
      }}