2015-02-23 67 views
0

我在Meteorjs應用程序中合作,至少我在第一次嘗試。 我建立了一些非常簡單的模板來滿足我的需求。meteorjs環境,通過id獲取元素返回奇怪的值

在我的代碼中,碰巧我必須檢查輸入文本的值。 因此,我在該文本框上設置了一個事件。

這是文本輸入:

<input type="text" name="meName" id="mockupName" /> 
<input type="button" {{buttonDisabled}} id="mockupCreate" value="New Mockup" /> 

事件檢查文本值和禁用或啓用按鈕。非常直截了當。

這是事件:

'keydown #mockupName': function(e) { 
    if (e.target.value.trim() == '') { 
     Session.set('buttonDisabled','disabled'); 
    } else { 
     Session.set('buttonDisabled',''); 
    } 
    }, 

它的工作原理只是確定。

e.target具有對我的文本輸入的引用,並存儲了它的值。

現在我引用這個模板從另一個網頁,在一個大的模板,我寫道:

{{#if mockupSelected}} 
    <input type="button" id="sw_product" value="switch to product view" /> 
    {{> mockupEditor}} 
{{else}} 
    Select product from the left 
{{/if}} 

而實際上當mockupSelected返回true出現在我的模板。

該事件不再有效。

當事件火(以及它觸發)我做了console.log(e.target)

我漸漸之前:<input#mockupName>我輸入的參考。 現在我得到:Object { __impl4cf1e782hg__: <input#mockupName>, parentNode_: undefined, firstChild_: undefined, lastChild_: undefined, nextSibling_: undefined, previousSibling_: undefined, treeScope_: Object }

具有一系列屬性的對象,其中之一包含我的參考。

這是安裝流星包的列表:

meteor-platform 
natestrauser:cart 
http 
iron:router 
accounts-base 
accounts-password 
accounts-ui 
alanning:roles 
aldeed:autoform 
aldeed:collection2 
twbs:bootstrap 
jeremy:velocity-animate 
ajduke:bootstrap-tokenfield 
sergeyt:typeahead 
standard-app-packages 
babrahams:editable-text-wysiwyg-bootstrap-3 
differential:vulcanize 
dburles:collection-helpers 
fortawesome:fontawesome 
yogiben:admin 

我想知道我可以訪問到該文本輸入,考慮到我不知道密鑰和的getElementById將返回我一樣目的。

我可以遍歷所有的對象屬性,並測試其中一個值是否實際上是一個nodeElement類型的文本,但我不認爲這是一個解決方案。

誰能告訴我如何恢復正常行爲?

回答

0

我猜在這裏,但你嘗試過:

的.html

<input type="text" name="meName" id="mockupName" class="mockupName" /> 
<input type="button" {{buttonDisabled}} id="mockupCreate" value="New Mockup" /> 

的.js

'keydown .mockupName': function(e) { 
    if (e.target.value.trim() == '') { 
     Session.set('buttonDisabled','disabled'); 
    } else { 
     Session.set('buttonDisabled',''); 
    } 
    }, 

要麼,或嘗試改變控制(mockupname的名字 - > uniqueMockupName)。當有重複的字段名稱時,我曾經有過奇怪的行爲。

+0

嗯我甚至可以嘗試,但ID是唯一的。一旦我將該課程應用到其他地方,按班級選擇也會給我帶來意想不到的結果。 – AndreaBogazzi 2015-02-23 23:14:20