2013-06-04 58 views
4

我試圖禁用在線工作解決方案後未選擇文件時提交按鈕。這與腳本類型有關嗎?未選擇文件時禁用提交按鈕

這是我遵循的例子:disable submit button until file selected for upload

<html> 
     <head> 

     <script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"> 
      $(document).ready(
    function(){ 
     $('input:submit').attr('disabled',true); 
     $('input:file').change(
      function(){ 
       if ($(this).val()){ 
        $('input:submit').removeAttr('disabled'); 
       } 
       else { 
        $('input:submit').attr('disabled',true); 
       } 
      }); 
    }); 
     </script> 
</head> 

<body> 
    <form action="#" method="post"> 

      <input type="file" name="fileInput" id="fileInput" /> 
      <input type="submit" value="submit" disabled /> 


     </form> 
</body> 
</html> 
+2

jQuery庫你有沒有包含jQuery庫? –

+2

你的代碼工作正常 - http://jsfiddle.net/fUvvb/。您在這裏沒有包含jQuery庫,但是 – lifetimes

+0

嗨,選擇文件時發生了什麼?您是否希望按鈕僅在按下按鈕或文件被真正選中時啓用? – Jaay

回答

2

與jQuery宣佈,因爲jQuery是不包括嘗試:

<html> 
<head> 
    <script type="text/javascript" src="http://code.jquery.com/jquery-2.0.0.js"></script> 
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> 


      <script type="text/javascript"> 
       $(document).ready(
     function(){ 
      $('input:submit').attr('disabled',true); 
      $('input:file').change(
       function(){ 
        if ($(this).val()){ 
         $('input:submit').removeAttr('disabled'); 
        } 
        else { 
         $('input:submit').attr('disabled',true); 
        } 
       }); 
     }); 
      </script> 
    </head> 

    <body> 
     <form action="#" method="post"> 

       <input type="file" name="fileInput" id="fileInput" /> 
       <input type="submit" value="submit" disabled /> 


      </form> 
    </body> 
    </html> 
+0

這是您接受的答案嗎? @Eddy –

+0

嗨,感謝您的幫助。我試過編輯src,但它仍然無法正常工作。它可能是別的嗎?但這很奇怪。它正在Jsfiddle.net上工作 – Eddy

+0

對不起,在我的電腦功能,你想你做後提交? 該腳本需要使用Jquery的連接的Internet。 否則,複製http://code.jquery.com/jquery-2.0.0.js,粘貼在單詞或塊註釋並保存在電腦上使用,無需互聯網 –

0

包括在<head>

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"></script> 
+0

嗨,感謝您的幫助。我編輯了javascript部分。但是,它仍然無法正常工作。 – Eddy