修改资料时:从数据库获取了一个字符串:String str = "灯泡,照明装置";如何令下面的相应的checkbox被选中,考虑有多个<tbody><tr>
<td><input id="interest1" type="checkbox" name="Dengpao" value="灯泡" />
<label for="CheckBoxList2_0">灯泡</label>
</td>
<td><input id="interest1" type="checkbox" name="Zhaoming" value="照明装置" />
<label for="CheckBoxList2_4">照明装置</label>
</td>
<td><input id="interest1" type="checkbox" name="Sheji" value="灯饰设计" />
<label for="CheckBoxList2_7">灯饰设计</label>
</td>
</tr>
<td><input id="interest1" type="checkbox" name="Dengpao" value="灯泡" />
<label for="CheckBoxList2_0">灯泡</label>
</td>
<td><input id="interest1" type="checkbox" name="Zhaoming" value="照明装置" />
<label for="CheckBoxList2_4">照明装置</label>
</td>
<td><input id="interest1" type="checkbox" name="Sheji" value="灯饰设计" />
<label for="CheckBoxList2_7">灯饰设计</label>
</td>
</tr>
<script>
var str = "灯泡,照明装置"; //从后台获取的字符串,豆号分割成数组!
var arr=str.split(',');
window.onload=function(){
var input=document.getElementById("tb").getElementsByTagName("input");
for(var j=0;j<input.length;j++)
{if(input[j].type=="checkbox")
{
for(var i=0;i<arr.length;i++)
{
if(input[j].value==arr[i])
{
input[j].checked=true;
}
}
}
}
}</script><table id="tb">
<tbody><tr>
<td><input id="interest1" type="checkbox" name="Dengpao" value="灯泡"/>
<label for="CheckBoxList2_0">灯泡</label>
</td>
<td><input id="interest1" type="checkbox" name="Zhaoming" value="照明装置" />
<label for="CheckBoxList2_4">照明装置</label>
</td>
<td><input id="interest1" type="checkbox" name="Sheji" value="灯饰设计" />
<label for="CheckBoxList2_7">灯饰设计</label>
</td>
</tr>
</tbody>
</table>
<script type="text/javascript" src="jquery-1.4.4.js"></script>
<script type="text/javascript">
$(function(){
var str="灯泡,照明装置";
var result=str.split(",");
$("input[type=checkbox]").val(result);
})
</script>
</head><body>
<table id="tb">
<tbody><tr>
<td><input id="interest1" type="checkbox" name="Dengpao" value="灯泡"/>
<label for="CheckBoxList2_0">灯泡</label>
</td>
<td><input id="interest1" type="checkbox" name="Zhaoming" value="照明装置" />
<label for="CheckBoxList2_4">照明装置</label>
</td>
<td><input id="interest1" type="checkbox" name="Sheji" value="灯饰设计" />
<label for="CheckBoxList2_7">灯饰设计</label>
</td>
</tr>
</tbody>
</table>
</body>
<script type="text/javascript" src="jquery-1.4.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
var str="灯泡,照明装置";
var array=str.split(",");
for(var i=0,len=array.length;i<len;i++)
$(":checkbox[value='"+array[i]+"']").attr("checked",true);
});
</script>
</head><body>
<table id="tb">
<tbody><tr>
<td><input id="interest1" type="checkbox" name="Dengpao" value="灯泡"/>
<label for="CheckBoxList2_0">灯泡</label>
</td>
<td><input id="interest1" type="checkbox" name="Zhaoming" value="照明装置" />
<label for="CheckBoxList2_4">照明装置</label>
</td>
<td><input id="interest1" type="checkbox" name="Sheji" value="灯饰设计" />
<label for="CheckBoxList2_7">灯饰设计</label>
</td>
</tr>
</tbody>
</table>
</body>