一、表单输入框与单选值获取
直接通过form中属性name值获取
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
#show{ border:1px solid red;}
</style>
</head>
<body>
<form name="myform">
爱好:
<input type="radio" name="hobby" value="前端" checked/>前端
<input type="radio" name="hobby" value="全栈">全栈<br/>
简介:<input type="text" name="info">
</form>
<div id="show">
</div>
<input type="button" name="button" value="获取" onclick="print()" />
<script>
function print(){
var hob = myform.hobby.value
var inf = myform.info.value
document.getElementById("show").innerText=hob+":"+inf
}
</script>
</body>
</html>
二、单独输入框与单选获取
单选需要遍历获取选中值,输入框获取元素的value属性
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
#show{border:1px solid red;}
</style>
</head>
<body>
爱好:
<input type="radio" name="like" value="一" checked/>一
<input type="radio" name="like" value="二">二</br>
描述:<input type="text" id="inp" value="">
<div id="show">
</div>
<hr/>
<input type="button" name="button" value="获取" onclick="print()" />
<script>
function print(){
var hob = document.getElementsByName("like")
var inf = document.getElementById("inp").value
var hobMess
for(var i=0;i<hob.length;i++){
console.log(hob[i])
if(hob[i].checked){
hobMess=hob[i].value
}
}
document.getElementById("show").innerText=hobMess+":"+inf
}
</script>
</body>
</html>