仅供引用,我已经阅读了相关主题 Uncaught TypeError: Cannot read property 'toLowerCase' of undefined并尝试去实现这个想法。尽管如此,我还是得到了经典
Uncaught TypeError: Cannot read property 'toLowerCase' of undefined
错误,我不知道它来自代码中的哪一行,因为错误点在 jQuery 中。我的代码是
ReadinessColorSetter = (function () {
this.ColorToRange = {
'#f65314': [0, 30],
'#ffbb00': [31, 70],
'#7cbb00': [70, 100]
}
this.SetReadiness = function (ipt) {
// ipt: input element containing
var val = $(this).val(),
newcolor = "#FFF"; // default
for (var hexcode in this.ColorToRange) {
var range = this.ColorToRange[hexcode];
if (val >= range[0] && val < range[1]) {
newcolor = hexcode;
break;
}
}
$(ipt).parent().children().last().css('background-color', newcolor);
}
return this;
})();
// On page load, set the color of the readiness
$(function () {
$('input[class="completeness"]').each(function (el) {
ReadinessColorSetter.SetReadiness(this);
});
});
// When the readiness value is changed, set the color of the readiness
//$('input[class="completeness"]').change(function () {
//ReadinessColorSetter.SetReadiness(this);
//});
$(document).on('change', $('input[class="completeness"]'), function (el) {
ReadinessColorSetter.SetReadiness($(el.target));
});
$('#change-submitted').click(function () {
alert('Change submitter clicked'); // TEST
});
正如您所见,我已经注释掉了我认为的问题并尝试实现正确的修复。
关于这个问题有什么指导吗?
最佳答案
这似乎无效:
$(document).on('change', $('input[class="completeness"]'), function (el) {
//-----------------------^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^----it should be a string
正如你所看到的,你已经传递了一个 jquery 对象,而在描述中你应该看到它需要一个 css 选择器字符串,例如:
$(document).on('change', 'input.completeness', function (el) {
在方法中:
var val = $(ipt).val(),
if 条件应该是:
if (val >= range[0] && val <= range[1]) {
newcolor = hexcode;//--^^----------------should be less than equal instead
break;
}
关于javascript - 是什么导致我的 "Uncaught TypeError: Cannot read property ' toLowerCase' of undefined"错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35124460/