【问题标题】:Ajax hyperlinkingAjax 超链接
【发布时间】:2010-09-27 08:02:42
【问题描述】:

我在为超链接提供 Ajax 功能时遇到问题。我有文件Link.htmlGetCustomerdata.php。 HTML的主要功能是将数据发送到getCutomerData.php并显示flash为“成功”。

而且我也不想要超链接,

<a href="#"

相反,我需要它:

<a href=GetCustomer.php?id=<format>

可以清除吗?

链接.Html

<html>
    <head>
        <title>Customer Account Information</title>
        <script type="text/javascript">
            var url = "GetCustomerData.php?id="; // The server-side script
            function handleHttpResponse() {
                if (http.readyState == 4) {
                    if(http.status==200) {
                        var results=http.responseText;
                        document.getElementById('divCustomerInfo').innerHTML = results;
                    }
                }
            }

            function requestCustomerInfo() {
                var sId =10;
                document.getElementById("txtCustomerId").value;
                http.open("GET", url + escape(sId), true);
                http.onreadystatechange = handleHttpResponse;
                http.send(null);
            }

            function getHTTPObject() {
                var xmlhttp;

                if(window.XMLHttpRequest){
                    xmlhttp = new XMLHttpRequest();
                }
                else
                    if (window.ActiveXObject){
                        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
                        if (!xmlhttp){
                            xmlhttp=new ActiveXObject("Msxml2.XMLHTTP");
                        }
                    }
                return xmlhttp;
            }

            var http = getHTTPObject();
        </script>
    </head>

    <body>
        <a href="getCustomerData.php?id=10&onclick="requestCustomerInfo();return false;">Show Me</a>
        <div id="divCustomerInfo"></div>
    </body>
</html>

...

而我的 PHP 文件只是闪现成功消息:

getCustomerData.php

<?php
    echo "Success"
?>

【问题讨论】:

标签: php ajax hyperlink


【解决方案1】:

您将锚点 onclick 事件放置在 href 属性中。 onclick 应该写成一个单独的属性:

...
<a href="getCustomerData.php?id=10" onclick="requestCustomerInfo();return false;">Show Me</a>

为了防止链接重定向页面,需要停止点击事件执行链接默认动作。在你的函数调用中:

function requestCustomerInfo(event) {
    if (!event) var event = window.event;
    event.preventDefault();
    ...
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-07-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-19
    • 1970-01-01
    • 1970-01-01
    • 2014-03-02
    相关资源
    最近更新 更多