2014-09-30 53 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