我在我正在处理的网站上使用 require ,在结帐时,我遇到了问题。我正在尝试更改信用卡图像以显示激活的卡号是他们输入的任何数字。
但是,每次按下一个键时,我都会收到此错误:
Uncaught TypeError: object is not a function
它说它来自我的“控制器”的第 12 行,即:
var chkCard = new CreditImage($(this).val(), '.icon-credit-cards');
未捕获的类型错误:对象不是函数(重复 3 次)
这是我的“演示者”,又名 assets/scripts/presenters/CreditImage.js
require(
[
'jquery'
],
function ($) {
"use strict";
var CreditImage = function ($number, $el) {
// Match and define
var visa = $number.match(/^4[0-9]{12}(?:[0-9]{3})?$/);
var mastercard = $number.match(/^5[1-5][0-9]{14}$/);
var amex = $number.match(/^3[47][0-9]{13}$/);
var discover = $number.match(/^6(?:011|5[0-9]{2})[0-9]{12}$/);
var matched;
// Create matched var
if (visa) {
matched = "visa";
} else if (mastercard) {
matched = "mastercard";
} else if (amex) {
matched = "amex";
} else if (discover) {
matched = "discover";
}
if (matched) {
// Highlight the matched credit card
$("."+$el+":not("+$el+"-" + matched + ")").addClass($el);
$("."+$el).addClass($el+"-" + matched);
} else {
// No match, highlight them all
$(".+$el).removeClass().addClass($el);
}
return matched;
}
}
);
这是“控制器”:
require(
[
'jquery',
'assets/scripts/presenters/CreditImage'
],
function ($, CreditImage) {
'use strict';
$(function () {
// Credit card validation
$("#add-cc-card-number").bind("keyup", function () {
var chkCard = new CreditImage($(this).val(), 'icon-credit-cards'));
});
});
}
);