2014-09-30 126 views
0

几个小时以来,我一直在试图了解什么是错的。我的目的是在文本框填充后启用按钮。代码似乎很好,根据我的测试JSFiddle,但它仍然不能在我的服务器上工作。我错过了什么或者这是一个服务器问题(很难相信,因为JavaScript是客户端)? PS:我不是HTML专家,所以我不知道如何识别它的语法;我不知道如何识别它的语法;我不知道如何识别它的语法;我不知道如何识别它的语法;我不知道如何识别它的语法。如果它不可读,我很抱歉,并希望编辑帮助。谢谢。JQuery没有通过Google的CDN工作

<!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> 
+3

jsFiddle将javascript包装在一个文件准备就绪(以及onload)。您试图在代码运行时访问页面头部不存在的HTML元素,在'$(function(){\\ code here})中打包;' – OJay 2014-09-30 02:08:52

+0

您的意思是'function()'没有在我的html中定义,是吗? – 2014-09-30 02:12:15

+0

http://learn.jquery.com/using-jquery-core/document-ready/ – j08691 2014-09-30 02:21:03

回答

2

当浏览器读取您的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 here

+0

你的解释非常简单易懂,完全合理。链接也很有用,谢谢。 – 2014-09-30 02:35:49