简体   繁体   中英

form submit jQuery function doesn't work in js file

i have a MVC View when i use form sumbit function inside my view its work fine. but when i put my code insde the js file its stop working

why is that? and is there a solution?

<script type="text/javascript" src="@Url.Content( "~/Scripts/site/js.js" )" ></script>
@using( Html.BeginForm( "Index" , "Sample" , FormMethod.Post , new { id = "frm" } ) )
{
    <div>
        <p>
            <input type="text" id="username" name="username" />
        </p>
        <input type="submit" value="Save" />
    </div>
    <script type="text/javascript">
        $('form').submit(function ()  // or $('#frm').submit
        {
            alert('1');
            return false;
        });
    </script>
}

In your external file, try wrapping it in a $(document).ready(); call. So

$(document).ready(function() {
    $('#frm').submit(function ()
        {
            alert('1');
            return false;
        });
    });

Also, I assume this is obvious, but remove the inline JS, too, when using the external js. :-)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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