meteorjs环境,通过id获取元素返回奇怪的值

时间:2015-02-23 21:16:30

标签: events meteor

我正在使用Meteorjs应用程序进行合作,这是我第一次尝试。 我构建了一些非常简单的模板以满足我的需求。

在我的代码中,我必须检查输入文本的值。 所以我在那个文本框上设置了一个事件。

这是文字输入:

<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包列表:

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返回给我相同的对象。

我可以迭代所有对象属性并测试其中一个值是否实际上是text类型的nodeElement,但我不认为这是一个解决方案。

谁能告诉我如何恢复正常行为?

1 个答案:

答案 0 :(得分: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 - &gt; uniqueMockupName)。当有重复的字段名称时,我有一次奇怪的行为。

相关问题