日期:2014-05-16  浏览次数:20347 次

获取form中button的值
大致的效果如下

HTML code

<form>
 <input name="textName1" type="text" id="textName1" />
 <input name="textName2" type="text" id="textName2" />
 <input type="radio" name="r1"value="测试1" checked>测试1<br>
 <input type="radio" name="r1"value="测试2" checked>测试2<br>
 <input type="button" name="Submit" value="Submit"  onclick=save() />
</form>


如何取到 button的值 function save()取数据 function manner()将数据在本页输出

------解决方案--------------------
PHP code
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
<script type="text/javascript">
        function save() {
                var form1 = document.getElementById('form1');
                var query = "";
                var hash = {}; 
                for (var i = 0; i != form1.length; ++ i) {
                        var obj = form1.elements[i];
                        var type = obj.type;
                        var name = obj.name;
                        var value = obj.value;
                        if (type == "text") {
                                if (query != "") {
                                        query += "&";
                                }
                                query += encodeURIComponent(name) + "=" + encodeURIComponent(value);
                        } else if (type == "radio") {
                                if (!hash[name] && obj.checked) {
                                        hash[name] = true;
                                        if (query != "") {
                                                query += "&";
                                        }
                                        query += encodeURIComponent(name) + "=" + encodeURIComponent(value);
                                }
                        }
                }
                alert(query);
        }
</script>
</head>
<body>
<form id="form1">
 <input name="textName1" type="text" id="textName1" />
 <input name="textName2" type="text" id="textName2" />
 <input type="radio" name="r1"value="测试1" checked>测试1<br>
 <input type="radio" name="r1"value="测试2" checked>测试2<br>
 <input type="button" name="Submit" value="Submit"  onclick="save()" />
</form>
</body>