簡體   English   中英

Onkeypress 不執行第二個函數調用

[英]Onkeypress not executing second function call

我在調試為什么沒有使用以下輸入標記調用 formatPhone() 函數時遇到問題(相同的代碼在其他腳本中正常工作——我已將其他腳本的工作代碼復制並粘貼到失敗的腳本中,但無濟於事)。 這些是我沒有編寫的現有函數,它們在我維護的整個系統中都得到了成功使用。 任何幫助都是最有幫助的。

HTML:

<input type="text" 
    name="cell_phone_txt" 
    id="cell_phone_txt" 
    maxlength="15" 
    style="width:100px;" 
    onkeypress="NumbersOnly(); return formatPhone(this, event);" 
    onkeyup="formatPhone(this);" 
    onchange="formatPhone(this);" 
    value="<?php echo $cell_phone_txt ?>" 
    placeholder="Cell Phone Number" 
>

function NumbersOnly()
{   
    if (window.event.keyCode > 57 | window.event.keyCode < 48)
    {   
        window.event.returnValue=false
    }
}


function formatPhone(elm, e) 
{
    alert();
    var keychar;
    if (e) 
    {
    var keynum;
    if (window.event) 
    {
        keynum = e.keyCode
    }
    else if (e.which) 
    {
        keynum = e.which
    }
    keychar = String.fromCharCode(keynum)
}   
if (/[\b]/.exec(keychar)) 
{
    return true;
} 
else 
{
    var p = elm.value + keychar;
    p = p.replace(/^[01]/,"");
    p = p.replace(/\D+/g, "");
    if (p.length > 0 && p.length < 3) 
    {
        p = "("+p;
    }
    else if (p.length >= 3 && p.length < 7) 
    {
        p = "("+p.substring(0,3)+") "+p.substring(3);
    }
    else if (p.length >= 7 && p.length < 10) 
    {
        p = "("+p.substring(0,3)+") "+p.substring(3,6)+"-"+p.substring(6);
    }
    else if (p.length) 
    {
        p = "("+p.substring(0,3)+") "+p.substring(3,6)+"-"+p.substring(6,10);
    }
        elm.value = p;
        return false;
}

}

由於我不知道您的函數應該做什么,因此這里有一個工作代碼片段,其中至少調用了這些函數。

 document.addEventListener('keypress', keyPressFunction); document.addEventListener('keyup', formatPhone); document.addEventListener('onchange', formatPhone); function keyPressFunction(e) { NumbersOnly(e); formatPhone(e); } function NumbersOnly(e) { if (e.keyCode > 57 | e.keyCode < 48) { return false; } } function formatPhone(e) { alert(); var keychar; if (e) { var keynum = e.keyCode; keychar = String.fromCharCode(keynum) } if (/[\\b]/.exec(keychar)) { return true; } else { var p = e.target.value + keychar; p = p.replace(/^[01]/, ""); p = p.replace(/\\D+/g, ""); if (p.length > 0 && p.length < 3) { p = "(" + p; } else if (p.length >= 3 && p.length < 7) { p = "(" + p.substring(0, 3) + ") " + p.substring(3); } else if (p.length >= 7 && p.length < 10) { p = "(" + p.substring(0, 3) + ") " + p.substring(3, 6) + "-" + p.substring(6); } else if (p.length) { p = "(" + p.substring(0, 3) + ") " + p.substring(3, 6) + "-" + p.substring(6, 10); } e.target.value = p; return false; } }
 <input type="text" name="cell_phone_txt" id="cell_phone_txt" maxlength="15" style="width:100px;" value="Hi" placeholder="Cell Phone Number">

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM