js表单提交后,如何获取表单中的数据并显示出来

如题所述

表单是用form来填写,在提交前可以获取表单里面的数据。我这里用jquery实现。

<html>
<body>
    <form method='post' action=''>
        <label for='inputText'>inputText</label>
        <input type='text' id='inputText'/>
        <br/>
        <label for='checkBox1'>checkBox1</label>
        <input type='checkbox' id='checkBox1'/>
        <br/>
        <label for='checkBox2'>checkBox2</label>
        <input type='checkbox' id='checkBox2'/>
        <!--其他你想填写的表单选项-->
        <input type='button' value='提交表单' id='submitBtn'/>
    </form>
    <script>
        $(document).ready(function()
        {
            $('#submitBtn').click(function(e)
            {
                /*一系列根据自己的意图判断输入框是否已输入内容并决定是否往下执行*/
                
                //获取表单的系列化数据。这会生成一个A=valueA&B=valueB这种形式的字符串
                var formData = $('form').serialize();
                $.post('目标地址',formData,成功/失败回调函数);
                e.preventDefault();
            });
            
        });
    </script>
</body>
</html>

温馨提示:答案为网友推荐,仅供参考
第1个回答  推荐于2017-10-10

可以通过在提交按钮上绑定点击事件来实现

如下

<scrippt>
    function show()
    {
        var sex=document.getElementById("sex").value;
    }
</scrippt>
性别<input id='sex' name='sex'/>
<input type='submit' value='submit' onclick='show()'/>

第2个回答  2013-05-16
保存住表单对象就行了
面向对象编程。本回答被提问者采纳
相似回答