代码之家  ›  专栏  ›  技术社区  ›  stormdrain

此元素为空

  •  0
  • stormdrain  · 技术社区  · 16 年前

    我正试图让一个自动完成表单工作,但我似乎不明白为什么我一直得到一个 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>
    

    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”手动设置textfield的值,我仍然会得到null。如果我尝试在controls.js中执行警报,它似乎在this.element=element;处失败;。e、 g.如果我提醒(元素),它会正确地提醒字段的id。如果我提醒(this.element)[在分配之后],它会提醒null。


    如果我改变

      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)
    
    4 回复  |  直到 16 年前
        1
  •  3
  •   Eugene    15 年前

    我不确定它是否仍然相关,但这个错误的原因很简单: 您可以执行js函数 OnLoad 事件。当然在那个阶段没有加载html元素。 把你的脚本放在html下面,或者调用它 OnPreRender 事件。

        2
  •  1
  •   stormdrain    16 年前

    问题是调用autocompleter的脚本在 <head> ... 需要在输入之后。

        3
  •  0
  •   James Black    16 年前

    为了更好地了解正在发生的事情,可以在IE8中使用Web Developer toolkit(对我来说,我点击F12)并进行调试,然后在create函数中插入一个中断,或者在Firefox中使用Firebug并进行调试。

    但是,我敢打赌这是你的问题: element = $(element);

    它正在查找该名称的元素类型。 element = $('input') 将获取页面上所有输入元素的数组。

    你可能想要 element = $('#' + element) ,因为如果您使用的是jquery,那么它将通过id获得,但我不确定您使用的是哪个库。

    所以,在赋值之前,先放一个断点,看看 element 转让前后。

        4
  •  0
  •   jim    16 年前

    我还没有让它正常工作,但我已经消除了错误。将Ajax.Autocompleter脚本放在页面底部,或者至少在定义控件之后。