请教一个select的问题

shenxian5 2009-11-20 09:23:51
表单每一组项目中有两个相同name的select:

<form name="form1" id="form1">
<select name="a1" id="a1"><option value="0"></option></select><select name="a1" id="a1"><option value="1"></option></select><input name="count" type="button" onclick="chkform()" />

<select name="a2" id="a2"><option value="1"></option></select><select name="a2" id="a2"><option value="0"></option></select><input name="count" type="button" onclick="chkform()" />

……

<select name="an" id="an"><option value="0"></option></select><select name="an" id="an"><option value="1"></option></select><input name="count" type="button" onclick="chkform()" />

</form>


每组后面的button触发chkform的时候,如何统计表单中有相同name的select的值?使其alert出来的结果用逗号隔开,如点击a1后面的button,得到a1的结果为:0,1 ;点击a2后的button,得到结果为1,0,一直循环下去?

也就是说chkform这个js函数应该怎么写呢?
...全文
76 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
xiaofangyanan 2009-11-20
  • 打赏
  • 举报
回复
顶上去
cntmi 2009-11-20
  • 打赏
  • 举报
回复

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title> New Document </title>
<meta name="Generator" content="EditPlus">
<meta name="Author" content="">
<meta name="Keywords" content="">
<meta name="Description" content="">
</head>

<body>
<script language="JavaScript">
<!--
function chkform(name){
var str ="";
var objs = document.getElementsByName(name) ;
for(var i=0;i<objs.length ;i++){
alert(objs[i].type);
if(objs[i].type.indexOf("select")>-1){
for(var n=0 ; n<objs[i].options.length ;n++){
if(objs[i].options[n].selected)
str += objs[i].options[n].value +",";
}
}
}
alert(str.substring(0,str.length-1)) ;
}
//-->
</script>
<form name="form1" id="form1">
<select name="a1" id="id1">
<option value="0">00000</option>
</select>

<select name="a1" id="id2">
<option value="1">11111</option>
</select>
<input name="count" type="button" onclick="chkform('a1')" value="得到a1的值"/>

<select name="a2" id="id3">
<option value="aaa">aaa</option>
</select>

<select name="a2" id="id4">
<option value="bbb">bbbb</option>
</select>
<input name="count" type="button" onclick="chkform('a2')" value="得到a2的值"/>
...........
</form>

</body>
</html>

shenxian5 2009-11-20
  • 打赏
  • 举报
回复
非常感谢!
草根醉秋意 2009-11-20
  • 打赏
  • 举报
回复

<select name="a1" id="a1">
<option value="0"></option>
</select><select name="a1" id="a1"><option value="1"></option>
</select><input name="count" type="button" onclick="chkform()" />

<script type="text/javascript">
function chkform() {
var sel = document.getElementsByName("a1");
var val = "";
for (var i = 0, len = sel.length; i < len; i++) {
val += ","+sel[i].value;
}
if (val.length > 0) {
val = val.substring(1);
}
alert(val);
}
</script>

ivws_19 2009-11-20
  • 打赏
  • 举报
回复
var selects=document.getElementsByName("");
var len=selects.length;
if(len>0){
for(var i=0;i<len;i++){
alert(selects[i].value);
}
}
shenxian5 2009-11-20
  • 打赏
  • 举报
回复
使用document.getElementById("a1").value这样似乎只能得到每组的第一个select值,如何构造循环使其每次点击统计的时候,统计这一组所有的同名select的值?使其结果类似asp接受表单时自动将同名的值变成0,1,2这样的形式?

87,993

社区成员

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

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