Pregunta

I have a simple javascript function

function SayTest() {
    alert("test");
}

I include the javascript file in my MVC 4 view

<script type="javascript" src="~/Scripts/MyScripts/MyScript.js"></script>

and call the SayTest function from within my view

<script type="text/javascript">
    $(document).ready(function () {
        $("#myBtn").click(function () {
            SayTest();
        });
    });
</script>

I'm getting an undefined function error. This is driving me nuts. If the js file was included and the function is define, why do I get an undefined function error?

¿Fue útil?

Solución

Try using :

<script type="text/javascript" src="~/Scripts/MyScripts/MyScript.js"></script>

Otros consejos

You probably need to include jQuery into your View page:

<script type="text/javascript" src="~/Scripts/jquery.min.js"></script>

Make sure that's before any code that calls $().

(Note: You might have to change the exact filename to match what's in your project).

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top