通过点击事件将 json 传递给 jQuery 函数

Pass json through click event to jQuery function

本文关键字:jQuery 函数 json 事件      更新时间:2023-09-26

我有一个旧的"封闭"系统,它在自己的容器中运行IE,这意味着在许多情况下我必须像穴居人一样编码,因为我无法使用任何浏览器开发人员工具/控制台对从远程系统返回的对象进行X射线检查。

现在,我正在查看的特定函数是来自第三方的回调(它变得复杂),它返回我愿意打赌的是标准的 JSON 对象。

function core_OnUeDeltaItemsUpdate(dataType, oData)
{
    if (dataType == "Units")
    {
        // Bail if dispatch report xml hasn't been loaded yet.
        if (oXml == null)
            return;
        ..... does lots of stuff
        // Reload the content in case any of our displayed units changed
        processUeDelta.click();
    }
}
... at the bottom of the page
<button style="display:none;" type="button" id="processUeDelta"/>

以及我希望使用jQuery的附加javascript文件

$(function(){
    $("#processUeDelta").click(function(){
        var i = 0;
        alert(this.ueDelta);
        for(var propertyName in this.ueDelta)
        {
            i++;
            alert("property " + i + ": " + oData[propertyName]);
        }
    });
});

现在,目前将自身绑定到隐藏按钮的最后一个函数无法解析 oData。我在这里被困在两件事上。

  1. 我不确定如何将 oData 对象传递给附加的事件处理程序
  2. 不太热衷于这种设计,也许还有另一种方法,我可以取出中间按钮,这样我就可以处理 JSON 数据对象 oData。

注意事项:

  • 这是基于数据泵,因此此回调平均每 5 秒调用一次。
  • 我只能使用 jQuery 1.7.1
  • 看不到对象,我的浏览器无法充当测试工具,移动部件太多,我无法从应用程序外部对其进行测试。

您可以将core_OnUeDeltaItemsUpdate函数替换为自己的函数,然后调用原始core_OnUeDeltaItemsUpdate函数。 在你的jQuery文件中做这样的事情

$(document).ready(function(){
    window._core_OnUeDeltaItemsUpdate = core_OnUeDeltaItemsUpdate;
    window.core_OnUeDeltaItemsUpdate = function(dataType, oData){
        // pass the parameters into the original function
        _core_OnUeDeltaItemsUpdate(dataType, oData);
        // do whatever you need to do with oData
        var i = 0;
        alert(this.ueDelta);
        for(var propertyName in this.ueDelta)
        {
            i++;
            alert("property " + i + ": " + oData[propertyName]);
        }
    }
});