jquery设置文本框值 与获取文本框的值

<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <title></title>
    <script src="../js/jquery-1.12.4.js"></script>
</head>
<body>
    <input type="text"/><br/>
    <input type="text"/><br/>
    <input type="text"/><br/>
    <input type="text"/><br/>
    <input type="text"/><br/>
    <input type="text"/><br/>
    <input type="text"/><br/>
    <input type="text"/><br/>
    <input type="text"/><br/>
    <input type="text"/><br/>
    <input type="button" id="btn1" value="设置"/>
    <input type="button" id="btn2" value="获取"/>
    <script>
        $(function () {
            //设置文本框的值
           $("#btn1").click(function () {
               $("input[type = 'text']").attr("value","文本$");
           });
            //获取文本框的值
            $("#btn2").click(function () {
                //定义一个空数组
                var value1 = [];
                for(var i =0;i<$("input[type = 'text']").length;i++){
                    //找到每一个input  这里input是dom对象
                    var input = $("input[type = 'text']")[i];
                    //用$(input)将它转换成jquery对象  使用attr()方法 获取值 追加到数组中去
                    value1.push($(input).attr("value"));
                }
                console.log(value1.join("|"));

            });
        });
    </script>
</body>
</html>

 

posted on 2016-06-25 14:53  LCFLY  阅读(2755)  评论(0编辑  收藏  举报

导航