2009-11-03 11 views
0

我想獲得一個自動完成表單工作,我似乎無法弄清楚爲什麼我總是得到一個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) 

回答

1

問題是腳本調用autocompleter在<head> ......需要在輸入之後。

+0

很高興你弄清楚了。 – 2009-11-03 18:10:06

0

爲了得到一個更好的想法發生了什麼,無論是在IE8使用Web開發工具包(用於我打F12),然後調試,然後在創建函數中暫停,或者使用Firebug執行相同的操作,然後調試。

不過,我敢打賭,這是你的問題: element = $(element);

它正在尋找該名稱的元素類型。 element = $('input')將獲得頁面上所有輸入元素的數組。

你可能想要 element = $('#' + element),因爲如果你正在使用jQuery,它會通過ID得到它,但我不確定那是你正在使用的庫。

因此,在您進行賦值之前就要設置一個斷點,並在賦值之前和之後查看element的值。

+0

據我所知,$(element)是getElementById的原型簡寫,所以實質上元素是傳遞給函數的參數。所以在我的例子中,param是'autocomplete',因此this.element試圖被分配'autocomplete',不是嗎? – stormdrain 2009-11-03 14:50:04

+0

此外,螢火蟲捕捉錯誤,但它本質上是相同的信息,它掛在'this.oldElementValue = this.element.value;'在controls.js中... – stormdrain 2009-11-03 14:53:53

+0

在通過id獲取它之前放置斷點,並在傳入函數時查看元素是否有值,然後查看後面會發生什麼。你正在使用哪個JavaScript庫? – 2009-11-03 15:00:04

0

我還沒有得到這個工作正常,但我已經消除了錯誤。將Ajax.Autocompleter腳本放在頁面的底部,或者至少在定義了控件之後。

+0

確實。我有一個叫''的腳本。我確實讓它工作,讓我知道如果我能幫忙。 – stormdrain 2010-02-09 16:58:59

3

我不確定它是否仍然相關,但此錯誤的原因可能非常簡單: 執行期間的Javascript無法找到該元素。 您執行js功能OnLoad事件。當然,在那個階段沒有加載html元素。 把你的腳本咆哮的HTML,或稱之爲OnPreRender事件。