Code works on JSFiddle but when saved locally doesn't work [duplicate]

假如想象 提交于 2019-12-13 04:23:22

问题


I am trying to take in input from the user and display it as a list. Thankfully someone has already done it and found it on JSFiddle. The codes works fine there but when I move it locally, it is not working. When I enter the text, nothing happens. I went through similar issues here but still not working.

Please advice if you spot the mistake. Thank you.

<!DOCTYPE HTML>
<html>
    <head>     
        <link rel="stylesheet" href="http://code.jquery.com/mobile/1.4.2/jquery.mobile-1.4.2.min.css" />
        <script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
        <script src="http://code.jquery.com/mobile/1.4.2/jquery.mobile-1.4.2.min.js"></script>

        <title>List maker</title>
        <style>
            a {
                margin-left: 20px;
                color: maroon;
                text-decoration: none;
                font-weight: bold;
            }
        </style>

        <script>
            $('form').submit(function () {
                if ($('input').val() !== '') {
                    var input_value = $('input').val();
                    $('ul').append('<li>' + input_value + '<a href="">x</a></li>');
            };
                $('input').val('');
                return true;
            });

            $(document).on('click', 'a', function (e) {
                e.preventDefault();
                $(this).parent().remove();
            });
        </script>
    </head>

    <body>
        <form>
            <label>Enter info and press enter</label>
            <input type="text" />
        </form>
        <ul></ul>
    </body>
</html> 

回答1:


Since your jquery code comes before your html, i think putting it in a $(document).ready() {} or moving it to the end of the body will make it work again.



来源:https://stackoverflow.com/questions/22435479/code-works-on-jsfiddle-but-when-saved-locally-doesnt-work

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!