javascript选定单选框选中的值,我这样写,返回的是[object HTMLCollection],是什么原因呢?
<!DOCTYPEhtml><html><head><metacharset="UTF-8"><title></title><scriptsrc="td_Sdk.js"t...
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
<script src="td_Sdk.js" type="text/javascript"></script>
<script type="text/javascript">
function getData(){
var dede=document.getElementById("Nameid").value; //玩家id
var den=document.getElementById("gameLv").value; //等级
var age=document.getElementById("gameAge").value; //玩家年龄
var name=document.getElementById("gameName").value; //昵称
//区服
var qufu = document.getElementsByTagName("gameServer");
for(var i=0; i<qufu.length; i ++){
if(qufu[i].checked){
alert(qufu[i].value);
}
}
//传入账户类型
var lei = document.getElementsByTagName("gameType");
for(var i=0; i<lei.length; i ++){
if(lei[i].checked){
alert(lei[i].value);
}
}
//性别
var sex = document.getElementsByTagName("gameSex");
for(var i=0; i<sex.length; i ++){
if(sex[i].checked){
alert(sex[i].value);
}
}
console.log(dede);
console.log(den);
console.log(qufu);
console.log(lei);
console.log(age);
console.log(name);
console.log(sex);
var dataObj={
gameId:dede, //玩家id
gameLv:den, //等级
gameServer:qufu, //区服
gameType:lei, //传入账户类型
gameAge:age, //玩家年龄
gameName:name, //昵称
gameSex:sex //性别
}
setData(dataObj);
}
</script>
</head>
<body>
<form name="input" action="" method="get">
<div>
<ul>
<li>玩家号: <input id="Nameid" type="text" ></li><br/>
<li>等 级: <input type="text" id="gameLv"></li><br/>
<li>
区 服:<ul>
<input type="radio" name="gameServer" id="gameServer" value="xs1" checked>一区<br>
<input type="radio" name="gameServer" id="gameServer" value="xs2">二区 <br>
<input type="radio" name="gameServer" id="gameServer" value="xs3">三区
</ul>
</li>
<li>账户类型:<ul><input type="radio" name="gameType" id="gameType" value="0" checked>匿名账号<br/>
<input type="radio" name="gameType" id="gameType" value="1">厂商账号
</ul>
<li>年 龄: <input type="text" id="gameAge"></li><br/>
<li>昵 称: <input type="text" id="gameName"></li><br/>
<li>性 别: <input type="radio" id="gameSex" name="gameSex" value="1" checked>男
<input type="radio" id="gameSex" name="gameSex" value="2">女
</li>
<li><input type="submit" value="提交" id="sub" onclick="getData()"></li>
</ul>
</div>
<br />
</body>
</html> 展开
<html>
<head>
<meta charset="UTF-8">
<title></title>
<script src="td_Sdk.js" type="text/javascript"></script>
<script type="text/javascript">
function getData(){
var dede=document.getElementById("Nameid").value; //玩家id
var den=document.getElementById("gameLv").value; //等级
var age=document.getElementById("gameAge").value; //玩家年龄
var name=document.getElementById("gameName").value; //昵称
//区服
var qufu = document.getElementsByTagName("gameServer");
for(var i=0; i<qufu.length; i ++){
if(qufu[i].checked){
alert(qufu[i].value);
}
}
//传入账户类型
var lei = document.getElementsByTagName("gameType");
for(var i=0; i<lei.length; i ++){
if(lei[i].checked){
alert(lei[i].value);
}
}
//性别
var sex = document.getElementsByTagName("gameSex");
for(var i=0; i<sex.length; i ++){
if(sex[i].checked){
alert(sex[i].value);
}
}
console.log(dede);
console.log(den);
console.log(qufu);
console.log(lei);
console.log(age);
console.log(name);
console.log(sex);
var dataObj={
gameId:dede, //玩家id
gameLv:den, //等级
gameServer:qufu, //区服
gameType:lei, //传入账户类型
gameAge:age, //玩家年龄
gameName:name, //昵称
gameSex:sex //性别
}
setData(dataObj);
}
</script>
</head>
<body>
<form name="input" action="" method="get">
<div>
<ul>
<li>玩家号: <input id="Nameid" type="text" ></li><br/>
<li>等 级: <input type="text" id="gameLv"></li><br/>
<li>
区 服:<ul>
<input type="radio" name="gameServer" id="gameServer" value="xs1" checked>一区<br>
<input type="radio" name="gameServer" id="gameServer" value="xs2">二区 <br>
<input type="radio" name="gameServer" id="gameServer" value="xs3">三区
</ul>
</li>
<li>账户类型:<ul><input type="radio" name="gameType" id="gameType" value="0" checked>匿名账号<br/>
<input type="radio" name="gameType" id="gameType" value="1">厂商账号
</ul>
<li>年 龄: <input type="text" id="gameAge"></li><br/>
<li>昵 称: <input type="text" id="gameName"></li><br/>
<li>性 别: <input type="radio" id="gameSex" name="gameSex" value="1" checked>男
<input type="radio" id="gameSex" name="gameSex" value="2">女
</li>
<li><input type="submit" value="提交" id="sub" onclick="getData()"></li>
</ul>
</div>
<br />
</body>
</html> 展开
2017-06-18
展开全部
qufu 和 sex你的赋值是对象啊,不是sex[i].value
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询