【问题标题】:Myscript not working in my localhost but if in jsfiddle it's fineMyscript 在我的本地主机中不起作用,但如果在 jsfiddle 中就可以了
【发布时间】:2016-07-19 14:32:19
【问题描述】:

我不知道我的代码有什么问题。如果我的脚本在 jsfiddle 中运行,那很好。但如果在我的计算机(本地主机)中它不起作用。

这是我的 jsfiddle:JSFIDDLE

这是我在电脑上运行的脚本

<html>
<body>
<input id="nominal" type="text" />
<script>
$(function() {
    var money = 20000;

    $("#nominal").on("change keyup", function() {
        var input = $(this);

        // remove possible existing message
        if( input.next().is("form") )
            input.next().remove();

        // show message
        if( input.val() > money )
            input.after("<form method=\"post\" action=\"\"><input type=\"submit\" name=\"yes\" value=\"Yes\"><input type=\"submit\" name=\"no\" value=\"No\"></form>");
    });
});
</script>
</body>
</html>

【问题讨论】:

  • 您需要将 jquery 包含到您的文件中
  • 本地主机中是否包含jQuery?
  • 我必须包含什么 jquery? @scrappedcola
  • @Eightleven 使用最新的:code.jquery.com/jquery.min.js

标签: javascript jquery html css localhost


【解决方案1】:

您需要包含 jQuery 库才能使用 jQuery 函数:

把它放在你的脚本标签上面:

<script   src="https://code.jquery.com/jquery-3.1.0.slim.js"   integrity="sha256-L6ppAjL6jgtRmfiuigeEE5AwNI2pH/X9IBbPyanJeZw="   crossorigin="anonymous"></script>

<script>
$(function() {
    var money = 20000;

    $("#nominal").on("change keyup", function() {
        var input = $(this);

        // remove possible existing message
        if( input.next().is("form") )
            input.next().remove();

        // show message
        if( input.val() > money )
            input.after("<form method=\"post\" action=\"\"><input type=\"submit\" name=\"yes\" value=\"Yes\"><input type=\"submit\" name=\"no\" value=\"No\"></form>");
    });
});
</script>

它会将 jQuery 从 CDN 加载到您的页面中。

【讨论】:

    猜你喜欢
    • 2020-09-19
    • 2020-11-25
    • 1970-01-01
    • 2015-12-15
    • 2015-09-25
    • 2016-10-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多