繁体   English   中英

jQuery无法通过Google的CDN工作

[英]JQuery not working through Google's CDN

几个小时以来,我一直试图了解问题所在。 我的目的是在填充文本字段后启用一个按钮。 根据我在JSFiddle上进行的测试,代码看起来还不错,但仍无法在我的服务器上运行。 我是否缺少某些东西,或者这是服务器问题(由于javascript是客户端,所以很难相信)?

PS:我不是HTML方面的专家,所以我不知道如何识别它的语法。 如果可读性不强,很抱歉,我们将感谢您提供的编辑帮助。 谢谢。

   <!DOCTYPE html>
    <html>
    <head>
      <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
      <link rel="stylesheet" href="css/style.css">

            <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
            <script type="text/javascript">
                var $input = $('input:text'),
                $apply = $('#apply');

                $apply.attr('disabled', true);
                $input.keyup(function() {
                    var trigger = false;
                    $input.each(function() {
                        if (!$(this).val()) {
                            trigger = true;
                        }
                    });
                    trigger ? $apply.attr('disabled', true) : $apply.removeAttr('disabled');
                });
            </script>
    </head>

      <body>
      <section class="container">
        <div class="OpenKore">
        <div id="absolute">
          <form method="GET" action="generate.php">
            <fieldset>
                <legend><h1>OpenKore Automatic Config:</h1></legend>
                LOGIN:
                <p><input type="text" id="id_login" name="login_value" value="" placeholder="Login"></p>
                SENHA:
                <p><input type="text" id= "id_senha" name="senha_value" value="" placeholder="Senha"></p>
                PIN:
                <p><input type="text" id="id_pin" name="pin_value" value="" placeholder="PIN"></p>

                <input id="apply" type="submit" name="commit" disabled value="Gerar Configurações">
            </fieldset>
          </form>
        </div>
        </div>
        </section>
        </body>
    </html>

当浏览器阅读您的HTML页面时,它从上到下阅读。 当到达您的<script>标签时,它将运行它们。 现在,我们要在到达页面的其余部分之前执行此操作,即在它甚至不知道任何bodyforminput:text标记之前就执行此操作,因此即使您的代码将运行,它也不会执行任何操作,因为页面上的元素还存在。

JavaScript 101,如果需要访问页面上的元素,则使代码在页面加载后运行。 你是怎样做的? 将代码放在页面底部(将<script>标记移到</body>标记之前),或将代码包装在浏览器完成页面加载后执行的函数中。 现在,jQuery为您提供了一种非常有用的方法,将一个函数传递给jQuery,它将在页面加载后执行。

jsFiddle自动为您执行此操作,因此左上角的下拉菜单显示“ onLoad”

即你的代码

$(); //this is the jQuery function


//This is your code wrapped in a function called 'yourCode'
function yourCode() {
    var $input = $('input:text'),
           $apply = $('#apply');

    $apply.attr('disabled', true);
    $input.keyup(function () {
        var trigger = false;
        $input.each(function () {
            if (!$(this).val()) {
                trigger = true;
            }
        });
        trigger ? $apply.attr('disabled', true) : $apply.removeAttr('disabled');
    });
}

$(yourCode); //this is passing the jQuery function a function, 
             //this will now be execute once the page is loaded

//or what most people do, pass in as an anonymous function 
//which eliminates a step

$(function () {
    var $input = $('input:text'),
             $apply = $('#apply');

    $apply.attr('disabled', true);
    $input.keyup(function () {
        var trigger = false;
        $input.each(function () {
            if (!$(this).val()) {
                trigger = true;
            }
        });
        trigger ? $apply.attr('disabled', true) : $apply.removeAttr('disabled');
    });
});

正如@ j08691所建议的,我建议在这里阅读有关jQuery中准备好的文档的信息

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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