Closure对象在内部函数中变为未定义

时间:2015-05-08 17:36:42

标签: javascript jquery

我玩过我的JS已经有一段时间了,所以我可能会遗漏一些完全明显的东西。我有以下内容:

MyApp.IaeCheckboxCallback = function ($trigger) {
    var $checkbox = $trigger.find('input[type="checkbox"]');

    $checkbox.on('click', function () {
        doIAECallbackNT($trigger.attr('id'), $trigger.data('data-recordid'), $trigger.data('data-recordtype'),
        $trigger.data('data-valuejs'), $trigger.data('data-statuselement'), $trigger.data('data-callback'))
    });
};

进入$ checkbox.on后,$ trigger变为未定义。我该如何防止这种情况?

添加更多相关代码

我正在和其他开发人员的另一个脚本文件中的JS联系。

function doIAECallbackNT
(
    iaeId,
    recordId,
    recordType,
    fieldName,
    value,
    statusElementId
)
{
    doIAECallbackNT(iaeId, recordId, recordType, fieldName, value, statusElementId, function(success) { });
}    

function doIAECallbackNT
(
    iaeId,
    recordId,
    recordType,
    fieldName,
    value,
    statusElementId,
    callback
)
{
    var req = newXMLHttpRequestNT ();
    var statusElement = document.getElementById(statusElementId);
    setStatusNT(statusElement, "Updating...");

    var msg = 
        "<?xml version='1.0' encoding='utf-8'?>" +
            "<soap:Envelope xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:soap='http://schemas.xmlsoap.org/soap/envelope/'>" +
                "<soap:Body>" +
                    "<HandleRequest xmlns='https://somesite.com/WebService/'>" +
                        "<iaeId>" + iaeId + "</iaeId>" +
                        "<recordId>" + recordId + "</recordId>" +
                        "<recordType>" + recordType + "</recordType>" +
                        "<fieldName>" + fieldName + "</fieldName>" +
                        "<value>" + escape(value) + "</value>" +
                    "</HandleRequest>" +
                "</soap:Body>" +
            "</soap:Envelope>";

    //alert(msg);

    req.onreadystatechange = function() {
        if (req.readyState == 4) {
            var success = false;
            try { success = (req.responseXML.documentElement.getElementsByTagName("Success")[0].text.toLowerCase() == "true"); }
            catch (e) { success = false; }

            var msg2 = "";
            //var msg3 = "";
            try { msg2 = req.responseXML.documentElement.getElementsByTagName("Message")[0].text; }
            catch (e) { msg2 = req.responseText; }
            //msg3 = msg2;

            if (!success) {                
                //Error: The string was not recognized as a valid DateTime. There is a unknown word starting at index 2. 
                if ( msg2.indexOf("valid DateTime") != -1 )
                    msg2 = "Failed: You entered an invalid date. Please re-enter the date.";

                if (msg2.indexOf("StartDate_Must_Be_Before_EndDate") != -1)
                    msg2 = "Failed: The Start date must be prior to the End date. Try changing the start date first.";

                //alert(msg2);
                AddFailedNote(msg2);

                if (statusElement.tagName.toLowerCase() != 'a') 
                    msg2 = "<span class='error bold'>" + msg2 + "</span>";
            }
            else if (statusElement.tagName.toLowerCase() != 'a') {
                AddSuccessNote(msg2);
                msg2 = "<span class='success bold'>" + msg2 + "</span>";
            }

            setStatusNT(statusElement, msg2);
            if (callback != null) { callback(success); }
        }
    };
    req.open("POST", "../Services/IAEService.asmx", true);
    req.setRequestHeader("Content-type", "text/xml; charset=utf-8");
    req.setRequestHeader("Content-length", msg.length.toString());
    req.setRequestHeader("SOAPAction", "https://www.somesite.com/WebService/HandleRequest");
    req.send(msg);
}

这是一个复选框的丑陋.NET渲染:

<span class="IaeCheckboxCallback" title="Ent?" data-recordid="11012" data-recordtype="Person" data-fieldname="HasEnterprise" data-statuselement="MainContent_MainContent_hrmol3_sp_ctl00" data-callback="function() { }" data-valuejs="MainContent_MainContent_hrmol3_dp_ctl00_0.checked" style="font-size:1em;"><input id="MainContent_MainContent_hrmol3_dp_ctl00_0" type="checkbox" name="ctl00$ctl00$MainContent$MainContent$hrmol3$dp$0$tc_HasEnterprise$ctl00" /></span>

另一个人的代码失败于:f(statusElement.tagName.toLowerCase()!='a')

我传入的所有参数在浏览器开发者工具中显示为未定义。

0 个答案:

没有答案