在JavaScript中获取input元素value的值:
方法一:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>JavaScript中获取input元素value值</title>
<link rel="stylesheet" href="">
<script>
function printInputValue() {
var inputValue = document.getElementById("demo1").value;
console.log(inputValue);
document.getElementById("demo2").innerHTML = inputValue;
}
</script>
</head>
<body>
<input type="text" id="demo1">
<button onclick="printInputValue()">获取input元素value值</button>
<p id="demo2"></p>
</body>
</html>
方法二:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>JavaScript34</title>
<link rel="stylesheet" href="">
<script>
function print(){
var a = myform.name.value;
alert(a);
document.getElementsByName("span")[0].innerHTML = a;
}
</script>
</head>
<body>
<form name="myform">
<input type="text" name="name" id="nn">
<input type="button" name="button" value="测试" onclick="print()">
<span name="span"></span>
</form>
</body>
</html>