我有一些用bootstrapSwitch
样式的复选框。
我写了一个脚本,在bootstrapSwitch
的状态为true时,将复选框的值添加到数组中。
以下是我的代码:
$('input[name^=skill]').on('switchChange.bootstrapSwitch', function (event, state) {
//alert(state);
//var s = [];
var s = new Array();
if( state === true )
{
//var value = $(this).val();
s.push( $(this).val() );
console.log(s);//(value)
}
console.log(s);//(value)
});
但是令人惊讶的是,push
方法替换了值,我的 s
数组始终只有一个索引。
您能告诉我为什么吗?
谢谢!
[](empty-array)
。 - Rayons
。尝试将s
的声明移到事件回调之外。 - haim770