首页 > 解决方案 > 如何在 nativescript 中获取文本字段值

问题描述

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

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

我得到的错误是:无法读取未定义的属性“文本”

请帮忙

标签: javascriptnativescript

解决方案


NativeScript 视图 XMLTextView

<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

推荐阅读