我有四个看起来类似的文件:
INDEX.XML
<Alloy>
<Window>
<View class="container">
<View id="report">
<!-- Adds a textfield for name entry -->
<Require type="view" src="textfield" id="name"/>
<Button id="checkNameValue" title="Check Value" width="100" height="40" onClick="checkname"/>
</View>
</View>
</Window>
</Alloy>
index.js
function checkname(e) {
alert("Your name is " + $.name.getView('nameTextField').value);
}
textfield.xml
<Alloy>
<View id="nameView">
</View>
</Alloy>
textfield.js
var nameTextField = Titanium.UI.createTextField({
hintText:"Type your name",
height:40,
width:300,
top:20,
borderStyle:Titanium.UI.INPUT_BORDERSTYLE_ROUNDED
});
$.nameView.add(nameTextField);
当我尝试单击Button来读取名称值时,出现错误:
message = "'undefined' is not an object (evaluating $.name.getView(\"nameTextField\").value')";
出了什么问题,我该如何解决?
答案 0 :(得分:2)
尝试将textfield.js更改为
$.nameTextField = Titanium.UI.createTextField({
hintText:"Type your name",
height:40,
width:300,
top:20,
borderStyle:Titanium.UI.INPUT_BORDERSTYLE_ROUNDED
});
$.nameView.add($.nameTextField);
然后您的警报代码应如下所示
alert("Your name is " + $.name.nameTextField.value);
答案 1 :(得分:0)
只需将Textfield添加到包含的文件中,就像
一样textfield.xml
<Alloy>
<View id="nameView">
<TextField value="" hintText="Type Here" id="textfield" />
</View>
</Alloy>
现在,您可以轻松地将文本字段访问索引文件:
index.js
alert($.name.textfield.value);