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

表单提交的问题
现在想完成一个功能,当按键执行后发送一个固定的字符串给服务器,不是那种填写后按submit提交的那种,希望能够给出一段程序的实例!

------解决方案--------------------
按钮不是有ID或者name吗,根据ID或者name初始化不同的url,在进行ajax post的时候提交到对应的url便可。

HTML code

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<script type="text/javascript">
    var urls = { "Button1": "/ajax/demo_get1.asp?fname=Bill&lname=Gates",
                "Button2": "/ajax/demo_get2.asp?fname=Bill&lname=Gates",
                "Button3": "/ajax/demo_get3.asp?fname=Bill&lname=Gates", 
                "Button4": "/ajax/demo_get4.asp?fname=Bill&lname=Gates" };
    function loadXMLDoc(t) {
        alert(urls[t.id]);
        var xmlhttp;
        if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
            xmlhttp = new XMLHttpRequest();
        }
        else {// code for IE6, IE5
            xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
        }
        xmlhttp.onreadystatechange = function () {
            if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
                document.getElementById("myDiv").innerHTML = xmlhttp.responseText;
            }
        }
        xmlhttp.open("GET", urls[t.id], true);
        xmlhttp.send();
    }
</script>
</head>
<body>

<h2>AJAX</h2>
<button type="button" id="Button1" onclick="loadXMLDoc(this)">请求数据</button>
<button type="button" id="Button2" onclick="loadXMLDoc(this)">请求数据</button>
<button type="button" id="Button3" onclick="loadXMLDoc(this)">请求数据</button>
<button type="button" id="Button4" onclick="loadXMLDoc(this)">请求数据</button>
<div id="myDiv"></div>

</body>
</html>