我如何获取本地脚本中的文本字段值

问题描述 投票:0回答:1

我正在尝试从nativescript核心的文本字段中获取文本。

let textField = args.object;
const yourSearch = textField.getViewById("requiredSkill").text;

我得到的错误是:无法读取未定义的属性'text'

请帮助

javascript nativescript
1个回答
0
投票

NativeScript视图XML TextView

<TextView editable="true" id="requiredSkill"></TextView>

带有tap事件的简单按钮

<Button text="Button" tap="{{ onButtonTap }}" />

该点击事件背后的JavaScript代码

var viewModel = observableModule.fromObject({
   onButtonTap: function (btargs) {
     let srcButton = btargs.object;
     const yourSearch =  srcButton.page.getViewById("requiredSkill").text;
   }
});

getViewById应该在<Page>模型上使用,以搜索整个页面或者您必须具有一个容器/布局,该容器/布局应具有一个ID,请首先使用getViewById查找布局,然后再次在该布局中执行另一个getViewById以查找TextView

因此回答您的问题,如果这是按钮单击事件,请获取按钮并获取按钮的页面并通过ID查找视图

srcButton.page.getViewById
© www.soinside.com 2019 - 2024. All rights reserved.