只有Javascript的本地脚本。我无法从TextField获取文本值。 getViewById返回未定义。我已经尝试了在stackoverflow和其他网站上建议的所有内容。我在做什么错?
我的XML是
<StackLayout class="form">
<Label class="header" text="Login"></Label>
<StackLayout class="input-field">
<TextField class="input" hint="Email" text="" id="emailFld"></TextField>
<StackLayout class="hr-light"></StackLayout>
</StackLayout>
<StackLayout class="input-field">
<TextField class="input" hint="password" text="" id="passFld"></TextField>
<StackLayout class="hr-light"></StackLayout>
</StackLayout>
<Button class="btn btn-primary" tap="login" text="Login"></Button>
</StackLayout>
我的Java语言是
const view = require("tns-core-modules/ui/core/view");
const getViewById = require("tns-core-modules/ui/core/view").getViewById;
function login(args) {
const page = args.object;
alert("button clicked!");
let un = view.getViewById(page, "emailFld");
let em = view.getViewById(page, "passFld");
console.log("Email = ", un.text);
console.log("Pass ", em.text);
}
exports.login = login;
错误是
System.err: An uncaught Exception occurred on "main" thread.
System.err: Calling js method onClick failed
System.err: TypeError: Cannot read property 'text' of undefined
System.err: StackTrace:
System.err: login(file: app\login.js:11:0)
System.err: at push.../node_modules/@nativescript/core/data/observable/observable.js.Observable.notify(file: node_modules\@nativescript\core\data\observable\observable.js:110:0)
System.err: at push.../node_modules/@nativescript/core/data/observable/observable.js.Observable._emit(file: node_modules\@nativescript\core\data\observable\observable.js:127:0)
System.err: at ClickListenerImpl.onClick(file: node_modules\@nativescript\core\ui\button\button.android.js:29:0)
System.err: at com.tns.Runtime.callJSMethodNative(Native Method)
System.err: at com.tns.Runtime.dispatchCallJSMethodNative(Runtime.java:1286)
System.err: at com.tns.Runtime.callJSMethodImpl(Runtime.java:1173)
System.err: at com.tns.Runtime.callJSMethod(Runtime.java:1160)
System.err: at com.tns.Runtime.callJSMethod(Runtime.java:1138)
System.err: at com.tns.Runtime.callJSMethod(Runtime.java:1134)
System.err: at com.tns.gen.java.lang.Object_vendor_15530_32_ClickListenerImpl.onClick(Object_vendor_15530_32_ClickListenerImpl.java:18)
System.err: at android.view.View.performClick(View.java:5198)
System.err: at android.view.View$PerformClick.run(View.java:21147)
System.err: at android.os.Handler.handleCallback(Handler.java:739)
System.err: at android.os.Handler.dispatchMessage(Handler.java:95)
System.err: at android.os.Looper.loop(Looper.java:148)
System.err: at android.app.ActivityThread.main(ActivityThread.java:5417)
System.err: at java.lang.reflect.Method.invoke(Native Method)
System.err: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
System.err: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
ActivityManager: Process org.nativescript.radiox5stereo (pid 9125) has died
尝试
let un = page.getViewById("emailFld");
并同时删除两者
const view = require("tns-core-modules/ui/core/view");
const getViewById = require("tns-core-modules/ui/core/view").getViewById;
您为什么不在此处使用标准Nativescript数据绑定?
如果您来自网络背景,很自然地希望有一个类似jQuery的界面,但是数据绑定更加有用。