手抄报 安全手抄报 手抄报内容 生活知识 生活百科 英语手抄报 清明节手抄报 节约用水手抄报 知识问答

jquery学习:[1]jQuery获取所有文本框的值

时间:2026-02-21 10:03:15

1、首相我们制作一个测试用的静态html页面。页面上面有5个文本输入框和一个按钮(参考效果图1)。

页码代码如下:

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

    <title>jquery获取所有文本框的值</title>

</head>

<body>

    文本框1:<input type="text" id="text1" /><br />

    <br />

    文本框2:<input type="text" id="text2" /><br />

    <br />

    文本框3:<input type="text" id="text3" /><br />

    <br />

    文本框4:<input type="text" id="text4" /><br />

    <br />

    文本框5:<input type="text" id="text5" />

    <input type="button" id="btnsee" value=" 查看 " /><br />

    <br />

    <span></span>

</body>

</html>

jquery学习:[1]jQuery获取所有文本框的值

2、页面制作完成了,我们要在html的<head>标签中引入 jquery-1.11.1.min.js 文件,然后,我们要给我按钮添加单击事件,获取用户输入文本框的文本,然后在按钮下面显示出来(参考效果图2)。

js代码如下:

    <script src="../res/js/jquery-1.11.1.min.js"></script>

    <script>

        /*页面加载时,给按钮绑定单击事件*/

        $(function () {

            $("input[type='button']").click(function () {

                /*循环获取文本框的值*/

                $("input[type='text']").each(function () {

                    $("span").append($(this).val() + ",");

                });

            });

        });

    </script>

jquery学习:[1]jQuery获取所有文本框的值

3、这样,我们的制作就完成了,jQuery获取输入框的值是不是很简单啊。

完整的页面代码如下:

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

    <script src="../res/js/jquery-1.11.1.min.js"></script>

    <script>

        /*页面加载时,给按钮绑定单击事件*/

        $(function () {

            $("input[type='button']").click(function () {

                /*循环获取文本框的值*/

                $("input[type='text']").each(function () {

                    $("span").append($(this).val() + ",");

                });

            });

        });

    </script>

    <title>jquery获取所有文本框的值</title>

</head>

<body>

    文本框1:<input type="text" id="text1" /><br />

    <br />

    文本框2:<input type="text" id="text2" /><br />

    <br />

    文本框3:<input type="text" id="text3" /><br />

    <br />

    文本框4:<input type="text" id="text4" /><br />

    <br />

    文本框5:<input type="text" id="text5" />

    <input type="button" id="btnsee" value=" 查看 " /><br />

    <br />

    <span></span>

</body>

</html>

© 2026 手抄报圈
信息来自网络 所有数据仅供参考
有疑问请联系站长 site.kefu@gmail.com