显示checkbox选中的记录

thinkme1314 2009-01-09 09:38:01
<html>
<body>
<title>复选显示</title>
<Script Language ="JavaScript">
function a(){
var hidstr="";
with(document.mainForm.elements["fruit"])
for(i=0;i<length;i++)
{
if(i==0){
hidstr=options[i].value
}else{
hidstr+=","+options[i].value
}
}
document.mainForm.elements["hid"].value=hidstr
alert(hidstr);
}
</Script>
<form method=post name="mainForm">
<input type=checkbox name=fruit value="">Banana<p>
<input type=checkbox name=fruit value="" checked>Apple<p>
<input type=checkbox name=fruit value="">Orange<p>
<input type=checkbox name=fruit value="">Pear<p>
<input type=checkbox name=fruit value="">Peach<p>
<input type=hidden name="hid" value="">
<input type=button value="提交" onclick="a();">
<input type=reset value="重置">
</form>
</body>
</html>

数组options为定义。怎么改啊?新手,弄不明白了。
...全文
125 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
yuqiangyuqiang 2009-01-09
  • 打赏
  • 举报
回复
<body>
<title>复选显示 </title>
<Script Language ="JavaScript">
function a(){
var hidstr="";
var fruit=document.getElementsByName("fruit");
for(i=0;i <fruit.length;i++)
{
if(fruit[i].checked)
{
hidstr+=fruit[i].value+",";
}

}
if(hidstr.charAt(hidstr.length-1)==',')
{
hidstr=hidstr.substring(0,hidstr.length-1);
}
document.mainForm.hid.value=hidstr
alert(hidstr);
}
</Script>
<form method=post name="mainForm">
<input type=checkbox name=fruit value="Banana">Banana <p>
<input type=checkbox name=fruit value="Apple" checked>Apple <p>
<input type=checkbox name=fruit value="Orange">Orange <p>
<input type=checkbox name=fruit value="Pear">Pear <p>
<input type=checkbox name=fruit value="Peach">Peach <p>
<input type=hidden name="hid" value="">
<input type=button value="提交" onclick="a();">
<input type=reset value="重置">
</form>
</body>
</html>
brmn9000 2009-01-09
  • 打赏
  • 举报
回复
顶下,学习
阿云ivan 2009-01-09
  • 打赏
  • 举报
回复
<div id="div1">
<input type="checkbox" name="fruit" value="Banana" />Banana
<p>
</p>
<input type="checkbox" name="fruit" value="Apple" checked="checked" />Apple
<p>
</p>
<input type="checkbox" name="fruit" value="Orange" />Orange
<p>
</p>
<input type="checkbox" name="fruit" value="Pear" />Pear
<p>
</p>
<input type="checkbox" name="fruit" value="Peach" />Peach
<p>
</p>
<input type="hidden" name="hid" value="" />
<input type="button" value="提交" onclick="a();" />
<input type="reset" value="重置" />
</div>



function a()
{
var hidstr="";
var cbs=document.getElementById('div1').getElementsByTagName('input');
for(i=0;i <cbs.length;i++)
{
if(cbs[i].type!='checkbox') continue;
if(!cbs[i].checked) continue;
hidstr+=cbs[i].value+",";
}
if(hidstr.length>0)
hidstr=hidstr.substring(0,hidstr.lastIndexOf(','));

alert(hidstr);
}

87,902

社区成员

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

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