我想獲得一個自動完成表單工作,我似乎無法弄清楚爲什麼我總是得到一個this.element
是空錯誤。autcomplete this.element爲空
這裏是JS:
//autocomplete
function AutoComp() {
new Ajax.Autocompleter("autocomplete", "autocomplete_choices", "fin/autocomplete", {});
}
document.onLoad = AutoComp();
HTML:
<input type="text" id="autocomplete" name="autocomplete_parameter"/>
<span id="indicator1" style="display: none">
<img src="/shared/img/loading.png" alt="Working..." />
</span>
<div id="autocomplete_choices" class="autocomplete"></div>
當網頁加載完畢後,我立即得到this.element是controls.js的這部分空錯誤:
var Autocompleter = { };
Autocompleter.Base = Class.create({
baseInitialize: function(element, update, options) {
element = $(element);
this.element = element;
this.update = $(update);
this.hasFocus = false;
this.changed = false;
this.active = false;
this.index = 0;
this.entryCount = 0;
this.oldElementValue = this.element.value;
如果我通過value =「blah」手動設置文本字段的值,我仍然會得到空值。如果我試圖在controls.js中做一個警報,它似乎在this.element = element;上失敗。例如如果我提醒(元素),它會正確提醒字段的ID。如果我在[分配後]提醒(this.element),它會提示爲空。
謝謝。
奇怪的行爲...
如果我改變
baseInitialize: function(element, update, options) {
element = $(element);
this.element = element;
this.update = $(update);
this.hasFocus = false;
this.changed = false;
this.active = false;
this.index = 0;
this.entryCount = 0;
this.oldElementValue = this.element.value;
到:
baseInitialize: function(element, update, options) {
test = $(element);
this.test = test;
this.update = $(update);
this.hasFocus = false;
this.changed = false;
this.active = false;
this.index = 0;
this.entryCount = 0;
this.oldElementValue = this.test.value;
它不會引發錯誤。 '元素'是保留的嗎?
我只是跑了Scriptaculous的單元測試,並有上自動完成測試一些故障:
failed testAjaxAutocompleter 7 assertions, 1 failures, 1 errors
Failure: 'ac_update' was not visible. undefined
TypeError: $("ac_update").firstChild is null(TypeError: $("ac_update").firstChild is null)
failed testAfterUpdateElement 2 assertions, 2 failures, 0 errors
Failure: 'ac2_update' was not visible. undefined
Failure: assertEqual: expected "'afterupdate:LI'", actual "'abcdefg'"
failed testTokenizing 1 assertions, 3 failures, 0 errors
Failure: assertEqual: expected "'test1'", actual "'abc'"
Failure: assertEqual: expected "'test1,test2'", actual "'abc,abc'"
Failure: assertEqual: expected "'test3,test2'", actual "'test1b,test2'"
failed testAjaxAutocompleterNoLinebreaksInResult 7 assertions, 1 failures, 1 errors
Failure: 'ac_update_br' was not visible. undefined
TypeError: $("ac_update_br").firstChild is null(TypeError: $("ac_update_br").firstChild is null)
很高興你弄清楚了。 – 2009-11-03 18:10:06