Consume jQuery functions from another file

0

I tell you, I'm trying to make a '' library '' of jquery functions so as not to reuse so much code, and to begin with, I'm trying with the following function.

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));
        });
    });
};

I have it alone in a file called myFunctions.js

It turns out that when I try to query it from jquery (in another file, inside a cshtml) it gives me the following error.

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

I tried to replace it with a function with the syntax

 function a(){
 alert("Hola");
 }

and I had no problem, read it correctly.

I'm trying to call it this way:

<script src="~/Scripts/misFunciones.js"></script>
$(document).ready(function () {

    $("#ref_primaria_positivo").soloNumerosDecimales();
});

However, the browser tells me that the function does not exist, I searched a lot on the internet, and I do not find people with this problem, does anyone know what it may be?

Thank you very much in advance!

EDIT I add the structure of my file.

** Cargo jquery, cldr and everything you need to use validate for Argentina

<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>


<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>

Then comes some cshtml generated by Visual Studio (nothing of js)

and there comes the part of the import and the jquery

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

  <script>

       $(document).ready(function () {

        $("#ref_primaria_positivo").soloNumerosDecimales();


      });
  </script>

The file that I include       myFunctions.js Its only content is the function I show above

Thanks guys!

EDIT: I forgot something important, and that is that if I declare the function inside the         where I use jQuery works perfectly, the problem occurs when I want to import it from an external .js.

    
asked by Juan Salvador Portugal 05.03.2018 в 19:30
source

1 answer

1

We are taking out the document ready with a spartan kick: D

<script>

       //$(document).ready(function () {

        $("#ref_primaria_positivo").soloNumerosDecimales();    

      //});

</script>
    
answered by 05.03.2018 / 20:15
source