请教一个js数组遍历判断是否包含的问题

wszdw142 2011-03-01 05:35:24
数组 AllList 的值为 "1,2,3"
数组 SelList 的值为 "1,3"
现在用for输出,如果AllList数组中的值有在SelList中出现的话,复选框打钩。

<script>
AllList = "1,2,3";
AllList = AllList.split(",");
SelList = "1,3";
SelList = SelList.split(",")
for(i = 0; i<AllList.length; i++){
document.write("<input name='' type='checkbox' value='' />"+AllList[i]+"<br>");
}
</script>
...全文
1258 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
浴火_凤凰 2011-03-02
  • 打赏
  • 举报
回复
需求不是很清楚
Ariels 2011-03-02
  • 打赏
  • 举报
回复
源代码

<html>
<head>
<title></title>
<script type="text/javascript" language="javascript">
function showCheck() {
var str = "";
AllList = "1,2,3";
AllList = AllList.split(",");
SelList = "1,3";
SelList = SelList.split(",")
for (i = 0; i < AllList.length; i++) {
var isExsit = 0;
for (var j = 0; j < SelList.length; j++) {
if (SelList[j] == AllList[i]) {
isExsit = 1;
str += "<input name='' type='checkbox' value='' checked />" + AllList[i] + "<br>";
break;
}
}

if(isExsit == 0)
str += "<input name='' type='checkbox' value='' />" + AllList[i] + "<br>";
}

document.getElementById("showCheck").innerHTML = str;
}
</script>
</head>
<body onload="showCheck()">
<div id="showCheck">asdfghjkjhs</div>
</body>
</html>
pawpaw 2011-03-02
  • 打赏
  • 举报
回复
加个检查函数循环检查某值是否在数组里就可以了。
AllList和SelList都是排好序的,其实可以不需要双重循环检查的,只不过实现稍稍复杂了一些。
function dataInArray(val, ary)
{
for (var i = 0; i < ary.length; i ++)
{
if (val == ary[i])
return true;
}
return false;
}
function test()
{
var AllList = "1,2,3";
AllList = AllList.split(",");
var SelList = "1,3";
SelList = SelList.split(",");
for(var i = 0; i<AllList.length; i++)
{
var checked = dataInArray(AllList[i], SelList) ? "checked" : "";
document.write("<input name='' type='checkbox' value='' " + checked + "/>"+AllList[i]+"<br>");
}
}
wszdw142 2011-03-02
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 cj205 的回复:]

既然值都是整形的。那何等的简单
var checkboxes;
for(var i in AllList) {
checkboxes[+AllList[i]] = "<input type='checkbox' />" + AllList[i] + "<br />";
}
for(var i in SelList) {
checkboxes[+AllList[i]] = "<……
[/Quote]
而且不是整形的,那些123是可以字母和数字组合的
wszdw142 2011-03-02
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 hsboy86 的回复:]

<script>
AllList = "1,2,3";
AllList = AllList.split(",");
SelList = "1,3";
SelList = SelList.split(",");
var vpass=false,che="";
for(i = 0; i<AllList.length; i++){
vpass=false;
f……
[/Quote]

这个我也测试了,所有复选框都被选择了
wszdw142 2011-03-02
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 cj205 的回复:]

既然值都是整形的。那何等的简单
var checkboxes;
for(var i in AllList) {
checkboxes[+AllList[i]] = "<input type='checkbox' />" + AllList[i] + "<br />";
}
for(var i in SelList) {
checkboxes[+AllList[i]] = "<……
[/Quote]
我测试了,不行。。出来的是空白页面
wszdw142 2011-03-02
  • 打赏
  • 举报
回复
6,7楼的方法都可以
1,2楼的也许是我不会用吧,同样感谢!
HSBOY86 2011-03-01
  • 打赏
  • 举报
回复
<script>
AllList = "1,2,3";
AllList = AllList.split(",");
SelList = "1,3";
SelList = SelList.split(",");
var vpass=false,che="";
for(i = 0; i<AllList.length; i++){
vpass=false;
for(var j in SelList)
{
if(AllList[i]==j)
vpass=true
}
if(vpass){che=" checked='true' ";}else{checked='false'}
document.write("<input name='' type='checkbox' value='' "+che+" />"+AllList[i]+"<br>");
}
</script>
Mr-Jee 2011-03-01
  • 打赏
  • 举报
回复
既然值都是整形的。那何等的简单
var checkboxes;
for(var i in AllList) {
checkboxes[+AllList[i]] = "<input type='checkbox' />" + AllList[i] + "<br />";
}
for(var i in SelList) {
checkboxes[+AllList[i]] = "<input type='checkbox' checked='checked' />" + AllList[i] + "<br />";
}
document.write(checkboxes.join(''));

代码只是提供思路,没有测试。

87,907

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 JavaScript
社区管理员
  • JavaScript
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧