【问题标题】:onkeypress event not working in firefox ? how to fix it? [duplicate]onkeypress 事件在 Firefox 中不起作用?如何解决? [复制]
【发布时间】:2012-11-12 11:52:33
【问题描述】:

可能重复:
onKeyPress event not working in Firefox

<div><textarea maxlength="1000" rows ="5" cols ="5" 
   style="width:398px; height: 175px; overflow: auto; padding: 4px !important" 
   name="body" value="" id="body" onkeypress="imposeMaxLength(this, 10);"  
   onPaste="imposeMaxLength(this, 10);" /></textarea></div>
<script>
function imposeMaxLength(field, MaxLen){

    if (field.value.length > (MaxLen-1)) {
        alert("It cannot exceed " + MaxLen + " characters.");
        field.value= field.value.substr(0, (MaxLen);
    }
}
</script>

【问题讨论】:

    标签: javascript


    【解决方案1】:

    您的标记不正确

    <textarea ... /></textarea>
    

    无效,应该是

     <textarea ... ></textarea>
    

    试试这个,效果很好

    <div>
      <textarea maxlength="1000" 
                rows="5"
                cols="5" 
                style="width: 398px; height: 175px;overflow: auto; padding: 4px !important"              
    name="body" 
                id="body" 
                onkeypress="imposeMaxLength(this, 10);"
                onpaste="imposeMaxLength(this, 10);" >
        </textarea>
    </div>
    
    <script type="text/javascript">
    function imposeMaxLength(field, MaxLen){
        if (field.value.length > (MaxLen-1)) {
            alert("It cannot exceed " + MaxLen + " characters.");
            field.value = field.value.substr(0, (MaxLen));
       }
    }
    </script>
    

    【讨论】:

    • 你用的是哪个浏览器?
    • 我也在使用 FF 14.0 它工作正常。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-05-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-09
    相关资源
    最近更新 更多