更改本地函数以便可以在全局范围内使用

我有一个测试功能,当前可以在输入信用卡号时对其进行格式化和测试


我需要这样做,而不是使用document.getElementById("cc")我可以使用传递给函数的变量,该变量等于文本框的 id,并且仍然会更改文本框中的内容


<script>

Function ReplacingImage() {

    var str = document.getElementById("cc").value;

    var strTest = str.charAt(0);

    switch(strTest) {

            case "3":

                    document.getElementById("x").src="/common/cc-images/amex.png"

                    break;

            case "4":

                    document.getElementById("x").src="/common/cc-images/visa.png"

                    break;

            case "5":

                    document.getElementById("x").src="/common/cc-images/mastercard.png"

                    break;

            case "6":

                    document.getElementById("x").src="/common/cc-images/discover.png"

                    break;

            case "":

                    document.getElementById("x").src="/common/cc-images/none.png"

                    break;

            default:

                    document.getElementById("x").src="/common/cc-images/invalid.png"

                    break;

    }


    var strNoSpace = str;

    var i;

    for(i = 0; i < (strNoSpace.length / "4"); i++) {

            strNoSpace = strNoSpace.replace(" ", "");

    }


    var lastDig = strNoSpace.substring(strNoSpace.length - 1, strNoSpace.length);


    if(isNaN(lastDig)) { document.getElementById("cc").value = str.substring(0, str.length - 1);  }


    if((strNoSpace.length > 16 && (strTest == "4" || strTest == "5" || strTest == "6")) || (strNoSpace.length > 15 && (strTest == "3"))) {

            document.getElementById("cc").value = str.substring(0, str.length - 1);

    } else if((strNoSpace.length == "15" && strTest == "3") || (strNoSpace.length == "16" && (strTest == "4" || strTest == "5" || strTest == "6"))) {

            value = document.getElementById("cc").value;

            if (/[^0-9-\s]+/.test(value)) {

                    document.getElementById("cc").style.color = "red";



智慧大石
浏览 83回答 1
1回答

炎炎设计

function ReplacingImage(id) {console.log(id);&nbsp; &nbsp; var str = document.getElementById(id).value;&nbsp; &nbsp; var strTest = str.charAt(0);&nbsp; &nbsp; console.log(str);&nbsp; &nbsp; switch(strTest) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case "3":&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; document.getElementById("x").src="/common/cc-images/amex.png"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case "4":&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; document.getElementById("x").src="/common/cc-images/visa.png"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case "5":&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; document.getElementById("x").src="/common/cc-images/mastercard.png"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case "6":&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; document.getElementById("x").src="/common/cc-images/discover.png"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case "":&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; document.getElementById("x").src="/common/cc-images/none.png"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; default:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; document.getElementById("x").src="/common/cc-images/invalid.png"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; }&nbsp; &nbsp; var strNoSpace = str;&nbsp; &nbsp; var i;&nbsp; &nbsp; for(i = 0; i < (strNoSpace.length / "4"); i++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; strNoSpace = strNoSpace.replace(" ", "");&nbsp; &nbsp; }&nbsp; &nbsp; var lastDig = strNoSpace.substring(strNoSpace.length - 1, strNoSpace.length);&nbsp; &nbsp; if(isNaN(lastDig)) { document.getElementById(id").value = str.substring(0, str.length - 1);&nbsp; }&nbsp; &nbsp; if((strNoSpace.length > 16 && (strTest == "4" || strTest == "5" || strTest == "6")) || (strNoSpace.length > 15 && (strTest == "3"))) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; document.getElementById(id).value = str.substring(0, str.length - 1);&nbsp; &nbsp; } else if((strNoSpace.length == "15" && strTest == "3") || (strNoSpace.length == "16" && (strTest == "4" || strTest == "5" || strTest == "6"))) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; value = document.getElementById(id").value;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (/[^0-9-\s]+/.test(value)) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; document.getElementById(id").style.color = "red";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; let nCheck = 0, bEven = false;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; value = value.replace(/\D/g, "");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for (var n = value.length - 1; n >= 0; n--) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var cDigit = value.charAt(n),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; nDigit = parseInt(cDigit, 10);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (bEven && (nDigit *= 2) > 9) nDigit -= 9;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; nCheck += nDigit;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; bEven = !bEven;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if((nCheck % 10) != 0) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; document.getElementById(id").style.color = "red";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; document.getElementById(id).style.color = "green";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; document.getElementById(id).style.color = "black";&nbsp; &nbsp; }&nbsp; &nbsp; if(strNoSpace.length > 15 && (strTest == "3")) { document.getElementById(id).value = str.substring(0, str.length - 1); }&nbsp; &nbsp; if(!isNaN(lastDig) && strNoSpace.length != "0" && (strNoSpace.length %&nbsp;"4") == "0" && strNoSpace.length < "16" && event.keyCode != "8") {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; document.getElementById(id).value += " ";&nbsp; &nbsp; }}<input type="text" onkeyup="ReplacingImage(this.id)" title="CC of Customer" id="cc" name="cc" placeholder="###">
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Html5