用js获取input的值,有一个按钮叫弹出,点...">
美文网首页
作业:获取input中的value的值

作业:获取input中的value的值

作者: 那一份美好 | 来源:发表于2018-03-02 18:15 被阅读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>

相关文章

网友评论

      本文标题:作业:获取input中的value的值

      本文链接:https://www.haomeiwen.com/subject/qttzxftx.html