类似于很多 SNS网站的一个邀请好友功能,点击一个好友背景就变色打上一个勾。我是用 数组的办法取好友的ID的。点击一个好友,给JS中的数组赋上它的ID,然后再取消选中时,就删除数组中的ID,但是在FF上测试不通过。
我是把循环出来的每个好友上的DIV的name、ID 设成好友的ID值。然后点击div的时候就传送ID到函数中。
JS代码如下 Array.prototype.indexOf = function(val) {
for (var i = 0; i < this.length; i++) {
if (this[i] == val) return i;
}
return -1;
};
Array.prototype.remove = function(val) {
var index = this.indexOf(val);
if (index > -1) {
this.splice(index, 1);
}
};
function sele(_this){
if(_this.style.backgroundImage==""){
++;
set(_this.id);
_this.style.background='#C0C0C0';
_this.style.backgroundImage="url(images/.png)";
_this.style.backgroundRepeat="no-repeat";
_this.style.backgroundPosition="right top";
}else{
--;
unset(_this.name.value);
_this.style.background='';
_this.style.backgroundImage="";
}
}
function set(id){
if(fs==null){
fs = new Array();
}
var len = fs.length;
fs[len+1]=id;
}
function unset(id){
var index = fs.indexOf(id);
if(index!=-1){
fs.remove(index);
}
}
function putin(){
//给表单中的隐藏域赋值,
document.all.fs.value=fs;
}
不知道各位有没有好的办法取到最终选中的好友ID?
我是把循环出来的每个好友上的DIV的name、ID 设成好友的ID值。然后点击div的时候就传送ID到函数中。
JS代码如下 Array.prototype.indexOf = function(val) {
for (var i = 0; i < this.length; i++) {
if (this[i] == val) return i;
}
return -1;
};
Array.prototype.remove = function(val) {
var index = this.indexOf(val);
if (index > -1) {
this.splice(index, 1);
}
};
function sele(_this){
if(_this.style.backgroundImage==""){
++;
set(_this.id);
_this.style.background='#C0C0C0';
_this.style.backgroundImage="url(images/.png)";
_this.style.backgroundRepeat="no-repeat";
_this.style.backgroundPosition="right top";
}else{
--;
unset(_this.name.value);
_this.style.background='';
_this.style.backgroundImage="";
}
}
function set(id){
if(fs==null){
fs = new Array();
}
var len = fs.length;
fs[len+1]=id;
}
function unset(id){
var index = fs.indexOf(id);
if(index!=-1){
fs.remove(index);
}
}
function putin(){
//给表单中的隐藏域赋值,
document.all.fs.value=fs;
}
不知道各位有没有好的办法取到最终选中的好友ID?
//数组
var fs;
//数组索引
var = 0;
另外最后提交 的时候可以去遍历所有的checkbox有没有打钩 打钩的取id然后提交
document.all.fs.value=fs;
用 getElementById如楼上所说,没必要高一个数组来保存。用户提交的时候,再来循环获取选中的好友编号!可以不用checkbox.div 设置几个属性就可以了好友ID
是否选中
var cids = [];
for ( var i = 0; i < allcb .length; i++) {
cids.push($(allcb[i]).val());
}
if (cids.length<1) {
alert('请选择邀请的好友。');
return false;
}