1

不幸的是,我无法创建 jsFiddle,因为这需要返回 HTML 的有效 URL,但关键是我正在使用输入掩码,它在执行后停止工作$.load() 我认为我只是不太了解这个函数。

脚本:

(function () {
    isa.uk.HpiLookup = (function () {
        function HpiLookup() { }
        HpiLookup.prototype.setupEventHandlers = function () {
            return $('#hpiLookupContainer').on("click", "#vehicleRegistrationSearchButton", this.onVehicleRegistrationSearchButton);
        };

        HpiLookup.prototype.setupInputMasks = function () {
            $('#VehicleRegistrationNumber').inputmask("#######", { "placeholder": "" });
        };

        HpiLookup.prototype.onVehicleRegistrationSearchButton = function (event) {
            event.preventDefault();
            return $('#hpiLookupContainer').load(
                GetVehicleByRegistrationNumberUrl,
                {
                    VehicleRegistrationNumber: $('#VehicleRegistrationNumber').val()
                },
                this.setupInputMasks);
        };

        return HpiLookup;

    })();
}).call(this);

索引.cshtml:

<div id="hpiLookupContainer">
    <input id="VehicleRegistrationNumber" type="text">
    <button id="vehicleRegistrationSearchButton" type="button">Search</button>
</div>

<script type="text/javascript">
    var GetVehicleByRegistrationNumberUrl = '@Url.Action("GetVehicleByRegistrationNumber", "Vehicle")';
    var hpiLookup = new isa.uk.HpiLookup();
    hpiLookup.setupEventHandlers();
    hpiLookup.setupInputMasks();
</script>

我试图以setupInputMasks这种方式进行 $.load() 回调,但它不会。之后如何重新应用事件绑定jQuery.load()

4

2 回答 2

0

当您调用this.setupInputMasks);它时,它是this在当前函数中而不是类中。Yuo 应该输入一些其他变量,即:selfthis输入。

您的代码中的探索:

//...
HpiLookup.prototype.onVehicleRegistrationSearchButton = function (event) {
        event.preventDefault();
        var self = this; // Now You put this (from a class) into another variable
        return $('#hpiLookupContainer').load(
            GetVehicleByRegistrationNumberUrl,
            {
                VehicleRegistrationNumber: $('#VehicleRegistrationNumber').val()
            },
            function(){self.setupInputMasks}); // now You call class this
    };
// ...

它应该帮助你。你也可以使用jQuery解决方案,函数代理。

更多在这里:http ://api.jquery.com/jQuery.proxy/

于 2013-03-21T12:41:02.600 回答
0

这是解决方案:

return $('#hpiLookupContainer').load(
    GetVehicleByRegistrationNumberUrl,
    {
        VehicleRegistrationNumber: $('#VehicleRegistrationNumber').val()
    },
    hpiLookup.setupInputMasks
);
于 2013-03-21T16:20:06.973 回答