作业:获取input中的value的值
2018-03-02 本文已影响0人
那一份美好
作业:<input id="study" value="666">
用js获取input的值,有一个按钮叫弹出,点击弹出,展示出input中的value的值?
解答:
- 按钮:
<input type="button" value="弹出" id="button">
- 获取页面的按钮:
document.getElementById("button")
- 给按钮绑定事件:
元素onclick= function(){ 执行事件 }
- 获取值:
document.getElementById("study").value
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<title>Examples</title>
<meta name="description" content="">
<meta name="keywords" content="">
<link href="" rel="stylesheet">
<style type="text/css">
</style>
</head>
<body>
<input id="study" value="666">
<input type="button" value="弹出" id="button">
<script>
document.getElementById("button").onclick = function () {
alert(document.getElementById("study").value)
}
</script>
</body>
</html>
引入变量:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<title>Examples</title>
<meta name="description" content="">
<meta name="keywords" content="">
<link href="" rel="stylesheet">
<style type="text/css">
</style>
</head>
<body>
<input id="study" value="666">
<input type="button" value="弹出" id="button">
<script>
var button = document.getElementById("button")
var study = document.getElementById("study")
button.onclick = function () {
alert(study.value)
}
</script>
</body>
</html>