JavaScript Titanium应用程序int赋值
JavaScript Titanium App int assignment
我使用Titanium编写JavaScript,用于移动应用程序开发。我在for循环中遇到了一个关于bn int计数的问题。button[bn].setTitle('*')在事件处理程序中创建的所有按钮上将始终为5,或者如果我取消注释//bn=0;,在for循环之外,它会将所有按钮更新为零值。
在我看来,它应该在创建时为每个按钮、事件处理程序等分配值,而不是在向前移动计数时返回并更改它。我在这里缺少了什么,或者需要做一些不同的事情?
/**
* create a view object to hold the buttons
* and add the buttons into the view
*/
function createRatingButtons(numButtons,BarTitle,topspace) {
// set vars
var bn=0;
var left = 5;
var top = 5;
/*
* create a view for the buttons
*/
var ratingView = Titanium.UI.createView({
height: 100,
color: 'white',
top: topspace,
});
/*
* create a label to put into the view
*/
var ratingLabel = Titanium.UI.createLabel({
text: BarTitle,
color: '#fff',
backgroundColor: 'transparent',
textAlign: 'left',
height: 'auto',
width: 'auto',
top: 0,
left: left,
})
ratingView.add(ratingLabel);
/*
* do the for loop and add
* the buttons to the view
*/
var button = [];
for(bn==0;bn<numButtons;bn++) {
button[bn] = Titanium.UI.createButton({
title: bn,
width: 50,
height: 50,
color: "black",
// backgroundColor: "blue",
left: left,
top: top+ratingLabel.getHeight(),
});
/*
* Add event handler for this button
*/
button[bn].addEventListener('click', function(e)
{
Ti.API.info("Rating Button Click #: " + bn);
/*
* Update buttons below this count for this object
* to have colored stars, and all starts after this
* to be uncolored.
*/
button[bn].setTitle('*')
});
ratingView.add(button[bn]);
left = left + 50 + 5;
}
//bn = 0;
// return the entire block for this view
return ratingView;
}
试试这个:
function createRatingButtons(numButtons, BarTitle, topspace) {
// set vars
var bn = 0;
var left = 5;
var top = 5;
// create a view for the buttons
var ratingView = Titanium.UI.createView({
height: 100,
color: 'white',
top: topspace,
});
// create a label to put into the view
var ratingLabel = Titanium.UI.createLabel({
text: BarTitle,
color: '#fff',
backgroundColor: 'transparent',
textAlign: 'left',
height: 'auto',
width: 'auto',
top: 0,
left: 20
});
ratingView.add(ratingLabel);
// add the buttons to the view
var button = [];
for(bn = 0; bn < numButtons; bn++) {
button[bn] = Titanium.UI.createButton({
title: bn,
width: 50,
height: 50,
color: "black",
left: left,
top: top+ratingLabel.getHeight()
});
// add event handler for buttons
button[bn].addEventListener('click', function(e) {
Ti.API.info("Rating Button Click #: " + e.source.title);
/*
* Update buttons below this count for this object
* to have colored stars, and all starts after this
* to be uncolored.
*/
e.source.setTitle('*');
});
ratingView.add(button[bn]);
left = left + 50 + 5;
}
// return the entire block for this view
return ratingView;
}
scrollView.add(createRatingButtons(5, "test1", 0));
scrollView.add(createRatingButtons(5, "test2", 95));
-
您正试图访问匿名函数中的bn。价值的bn将在执行匿名函数时进行评估。在届时,bn的价值将是numButtons变量。
因此,请避免在匿名函数中使用bn。你可以使用e.source获取对按钮的引用。
- 另外,我认为你错误地使用了
===
在你的循环。
for(bn==0;bn<numButtons;bn++)
相关文章:
- Javascript变量赋值|
- 无法为打字稿字典赋值
- Javascript-根据赋值顺序,按键合并对象数组
- Javascript:应为赋值或函数调用,但实际看到的却是表达式
- 如何通过json对象数组为嵌套对象赋值
- 赋值后的回调函数
- ||(OR)运算符如何在赋值中工作
- 可以使用属性赋值实现多个函数
- 将ECMAScript 6析构函数赋值(ES2015)重构为旧版本的javascript
- 如何在javascript中为全局变量赋值
- Javascript:JSHint:应为赋值或函数调用,但实际看到的却是表达式
- JavaScript:在嵌套循环中为数组赋值
- 未设置变量的赋值| jQuery的作用是什么
- Javascript赋值运算符的奇怪行为
- onchange赋值的Javascript位置
- 我可以在javascript中的回调函数中为变量赋值吗
- 逻辑(JSint友好)等效于while循环中的此赋值
- 为数组元素的属性赋值将设置所有其他类似元素的属性
- 错误-应为赋值或函数调用,但实际看到的却是表达式
- JavaScript Titanium应用程序int赋值