2015-09-27 54 views
1

有人在這裏問了同樣的問題How do you get the value from a TVML textField?,但沒有解釋完整的答案,我的評論被編輯,提示我問一個全新的問題。這裏是...如何在TVJS中獲取TVML文本字段的節點和鍵盤引用?

任何人都可以幫助理解如何最好地從TVML中獲得對textField節點及其鍵盤值的參考嗎?現在,我在下面使用這段代碼中Presenter.js才能到textField一旦用戶點擊該模板中Done按鈕顯示形式&鍵盤,但仍然得到

器iTM1:未定義不是一個對象(評估'textField.getFeature') - .../JS/Presenter.js

當我使用getFeature("Keyboard")

load: function(event) { 
var self = this, 
    ele = event.target, 
    templateURL = ele.getAttribute("template"), 
    presentation = ele.getAttribute("presentation"), 
    videoURL = ele.getAttribute("videoURL"); 

if (templateURL && (event.type == "select")) { 
    var formTemplate = ele.parentNode.parentNode; // that should give me <formTemplate> element 
    var children = formTemplate.childNodes; 
    var textField = children[1]; // which is the second element in <formTemplate><banner></banner><textField></textField>... 
    var keyboard = textField.getFeature("Keyboard"); // this raises the ITML Error... 
    ........................... 

UPDATE:

我找到了解決辦法通過查看accessing elements by traversing XML DOM

var formTemplate = ele.parentNode.parentNode; // your formTemplate button 
var children = formTemplate.childNodes; 
var textField = children.item(1); // replace "1" with the index of "textField" element in your template 
var keyboard = textField.getFeature("Keyboard"); // get the textField's Keyboard element 
var userValue = keyboard.text; // the value entered by the user on the Apple TV keyboard 

回答

0

假定自我解釋的解決方案, 我加入是完整的更改回調文字:

var keyboard = textField.getFeature("Keyboard"); 
keyboard.onTextChange = function() { 
    console.log("onTextChange "+keyboard.text) 
} 

並假設表格模板類似於

<formTemplate> 
    <banner> 
     <title>${formTitle}</title> 
     <description class="longDescriptionLayout">${formDescription}</description> 
    </banner> 
    <textField>${formPlaceholder}</textField> 
    <footer> 
     <button id="pairingCodeInput"> 
     <text>Confirm</text> 
     </button> 
    </footer> 
    </formTemplate> 

Presenter I類會增加處理更多的選擇是這樣的:

if ((event.type == "select")) { // item selected 
    if (itemId == "pairingCodeInput") { // form input 
     var formTemplate = ele.parentNode.parentNode; 
     var children = formTemplate.childNodes 
     var textField = children.item(1) 
     var keyboard = textField.getFeature("Keyboard") 
     keyboard.onTextChange = function() { 
      console.log("onTextChange " + keyboard.text) 
     } 
     var userValue = keyboard.text 
     console.log("Entered " + userValue) 

    } 
} 
1

如何起始於文檔的根?使用getElementsByTagName,getElementByTagNamegetElementById來訪問textField

var doc = navigationDocument.documents[navigationDocument.documents.length-1]; 
var textField = doc.getElementByTagName("textField") 

var value = textField.getFeature("Keyboard").text; 
0

您需要使用

xmlElem.item(i) 

,而不是

xmlElem.item[i] 
相關問題