I am trying to figure out how to get the value from the textField in TVML using TVJS but I haven't found any documentation on it yet and I don't want to wait around for the documentation to be updated. There isn't really any patterns for binding to input since I'm sure this is a seldom use case other than logging into a service. I guess I could parse the Xml but that seems super hacky.
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
回答1:
I ended up figuring it out myself. I had to use the callback from the select event object and then pass the text element into a function to handle the event.
function template() {
var template = "<document><formTemplate><textField id="email">Your email</textField><footer><button><text>Submit</text></button></footer></formTemplate></document>";
var parser = new DOMParser();
var doc = parser.parseFromString(template , "application/xml");
var email = doc.getElementById("email");
doc.addEventListener("select", function() { submit(email); });
}
function submit(textField) {
var keyboard textField.getFeature('Keyboard');
var email = keyboard.text;
}
Once I had the node I used the apple TVJS function getFeature('Keyboard') to get the text value from this object.
回答2:
this solution doesn't work for me by some reason, but I found another one:
var sender = event.target;
var formTemplate = sender.parentNode.parentNode;
var children = formTemplate.childNodes;
var textField = children.item(1); // replace "1" with the index of "textField" element in your template
var inputValue = textField.getFeature("Keyboard").text;