简体   繁体   English

jQuery 从其他文件调用 $.fn 函数

[英]jQuery calling $.fn function from other file

Hello i'm having an issue trying to read jQuery function from another file.您好,我在尝试从另一个文件读取 jQuery 函数时遇到问题。

misFunciones.js misFunciones.js

function formatString() {
var args = Array.prototype.slice.call(arguments, 1);
return arguments[0].replace(/\{(\d+)\}/g, function (match, index) {
    return args[index];
});
}


jQuery.fn.soloNumerosDecimales = function () {
    return this.each(function () {
        $(this).keydown(function (e) {
            var key = e.charCode || e.keyCode || 0;



            if (e.keyCode == 190 || e.keyCode == 110 || e.keyCode == 188) {
                e.preventDefault();
                $(this).val($(this).val() + ',');
            }


            return (
                (key == 65 || key == 86 || key == 67) && (e.ctrlKey === true || e.metaKey === true) ||
                key == 8 ||
                key == 9 ||
                key == 13 ||
                key == 46 ||
                key == 110 ||
                key == 190 ||
                (key >= 35 && key <= 40) ||
                (key >= 48 && key <= 57) ||
                (key >= 96 && key <= 105));
        });
    });
};    

The function works perfect when are inside the same file, but when i try to call it from another .js file i get该函数在同一个文件中运行完美,但是当我尝试从另一个 .js 文件调用它时,我得到

Uncaught TypeError: $(...).soloNumerosDecimales is not a function

Imports进口

<script src="~/Scripts/jquery-1.10.2.min.js"></script>
<script src="~/Scripts/cldr.js"></script>
<script src="~/Scripts/cldr/event.js"></script>
<script src="~/Scripts/cldr/supplemental.js"></script>
<script type="text/javascript" src="~/Scripts/globalize.js"></script>
<script type="text/javascript" src="~/Scripts/globalize/number.js"></script>
<script type="text/javascript" src="~/Scripts/jquery.validate.js"></script>
<script type="text/javascript" 
src="~/Scripts/jquery.validate.unobtrusive.js"></script>
<script type="text/javascript" src="~/Scripts/jquery.validate.globalize.js"></script>

Load cldr json加载 cldr json

<script>
    $.when(
        $.getJSON("/Scripts/cldr/supplemental/likelySubtags.json"),
        $.getJSON("/Scripts/cldr/supplemental/numberingSystems.json"),
        $.getJSON("/Scripts/cldr/numbers.json")
    ).then(function () {
        console.log("start slicing");
        return [].slice.apply(arguments, [0]).map(function (result) {
            console.log("slicing done");
            return result[0];
        });
    }).then(Globalize.load).then(function () {
        Globalize.locale("es-AR");
        console.log("Locale set to es-AR");
        }).then(console.log("LOADED EVERYTHING"));
</script>

Adding script of misFunciones.js添加 misFunciones.js 的脚本

<script src="~/Scripts/misFunciones.js"></script>

Trying to use it尝试使用它

 $(document).ready(function () {
 $("#ref_primaria_positivo").soloNumerosDecimales();
});

Thank you so much in advance and sory for my english :(非常感谢你,谢谢我的英语:(

The solution was call the function before the document.ready function解决方案是在 document.ready 函数之前调用该函数

  $("#ref_primaria_positivo").soloNumerosDecimales();
  $(document).ready(function () {
  )};

Thanks everyone!谢谢大家!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM