【发布时间】:2016-02-01 06:47:42
【问题描述】:
仅供参考,我已经阅读了相关线程Uncaught TypeError: Cannot read property 'toLowerCase' of undefined 并尝试实现这个想法。不过,我得到了经典的
未捕获的类型错误:无法读取未定义的属性“toLowerCase”
错误,我不知道它来自代码中的哪一行,因为 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
});
如您所见,我已经注释掉了我认为的问题并尝试实施正确的修复。
关于这个问题的任何指导?
【问题讨论】:
-
你能看到 jQuery 中有什么错误吗?什么时候出现错误?
-
@KeiranTai 出现在 pg 加载
-
这应该是
$(ipt).val()吗?在这里查看:this.SetReadiness = function (ipt) {...var val = $(this).val(), -
我不确定,但我认为 SetReadiness(this) 应该是 SetReadiness($(this));在我的一个函数中,我有一个“每个”调用,我总是做“var obj = $(this);”然后只使用“obj”,因为我一直忘记何时只使用“this”以及何时使用“$(this)”。我相信这就是“未定义”所指的。
-
@KeiranTai 是的。我的错。让我检查一下是否可以解决问题...
标签: javascript jquery dom