2015-06-15 149 views
1

我想選擇由ID的元素是動態創建的選擇動態元素:聚合物1.0 - 發行由ID

<dom-module id="filter-box"> 
    <style> 
    :host { 
     display: block; 
    } 
    </style> 
    <template> 
    <h4 id="title">{{title}}</h4> 

    <paper-checkbox id="test">test</paper-checkbox><br> 

    <template is="dom-repeat" items="{{filters}}"> 
     <paper-checkbox id="{{item}}">{{item}}</paper-checkbox><br> 
    </template> 

    </template> 
</dom-module> 
<script> 
    (function() { 
    Polymer({ 
     is: 'filter-box', 
     properties: { 
     filters: { 
      type: Array, 
      notify: true, 
     }, 
     title: { 
      type: String 
     } 
     }, 
     ready: function() { 
     this.filters = [ 
      'Commercial', 
      'Enterprise' 
     ]; 
     }, 
     isSelected: function(filter) { 
     return this.$$[filter].checked; 
     } 
    }); 
    })(); 
</script> 

當isSelected(「東西」)叫我得到:

"Uncaught TypeError: Cannot read property 'checked' of undefined" 

我可以通過這個。$。標題選擇標題,但是我不能通過這種方式選擇動態元素或者使用這個。$$作爲建議here

回答

1

根據你提供的參考文獻,你應該調用this.$$(selector)括號在函數調用(而不是括號)。所以用這個代替代碼:

return this.$$('#'+filter).checked; 

請注意,您可能還需要與#預先設置id選擇。

+0

感謝您的迴應,但是。$$(...)仍然返回undefined。 – Nick

+0

你試過這個。$$('#'+ filter).checked;'? Id選擇符以'#'開頭。 –

+0

謝謝!我在想'#'是隱含的。 – Nick