<script language="javascript">
function getCheckValues(theForm) {
var values = "";
var temp;
var formlen = theForm.length;
for (var i=0;i<formlen;i++) {
var theitem = theForm.elements[i].name;
var ischeck = theitem.indexOf("selectedObjectIds",0) + 1; /* simple string search on checkbox consistent name, you change it to deleteID or whatever */
if (ischeck > 0) {
if (theForm.elements[i].checked) {
//alert(values);
values += ((values==""?"":";")+theForm.elements[i].value);
}
}
}
alert(values);
return values;
}
function getCheckValues2(theForm){
var ids="";
for(var i=0;i<theForm.elements.length;i++){
if(theForm.elements[i].type=="checkbox"){
if(theForm.elements[i].checked==true){
ids += ((ids==""?"":";")+theForm.elements[i].value);
}
}
}
alert(ids);
}
</script>
<form name="theForm">
<table>
<tr>
<td><input type="checkbox" name="selectedObjectIds" value="id1"/></td>
</tr>
<tr>
<td><input type="checkbox" name="selectedObjectIds" value="id2"/></td>
</tr>
<tr>
<td><input type="checkbox" name="selectedObjectIds" value="id3"/></td>
</tr>
<tr>
<td><input type="button" value="测试checkbox选中并得到id" onclick="getCheckValues(theForm)"/></td>
<td><input type="button" value="测试checkbox选中并得到id" onclick="getCheckValues2(theForm)"/></td>
</tr>
</table>
</form>
本文介绍了一种使用JavaScript从HTML表单中获取所有被选中的复选框值的方法。通过两个不同的函数实现,可以方便地应用于网页交互场景。
1271

被折叠的 条评论
为什么被折叠?



