2011-11-24 62 views
1

我在Ruby on Rails中實現。我想要做什麼和Jquery一起工作。我想要做的只是:我有一個file_field_tag,讓用戶上傳文件,當用戶選擇一個文件時,提交按鈕已經啓用。所以只要他沒有選擇文件,用戶就不能點擊按鈕。我必須將Ruby on Rails中的JQuery放在哪裏?

這是JQuery的,我已經寫了

$(document).ready(
    function(){ 
     $('input:file').change(
      function(){ 
       if ($(this).val()) { 
        $('input:submit').attr('disabled',false); 
        // or, as has been pointed out elsewhere: 
        // $('input:submit').removeAttr('disabled'); 
       } 
      } 
      ); 
    }); 

,我的看法是這樣的:

<h2>Import projects and users</h2> 


<% form_tag({:action => 'match'}, {:multipart => true, :id => 'file_form_id'}) do %> 
<fieldset title="File"> 
    <legend>File</legend> 
<table> 
    <tr> 
    <td> 
     <label for="dump_file"> 
     Select a CSV File : 
     </label> 

     </td> 
     <td > 
      <%= file_field_tag 'file', :size => 500 %></p> 
     </td> 
     </tr> 
    </table> 

      <%= submit_tag 'Submit', :disabled => true -%> 


    <% end -%> 

我認爲這是正確的。但我必須把這個JavaScript?我真的不知道該把它放在哪裏。我已經閱讀將它放在application.js中,但是當我這樣做時,沒有任何反應。該按鈕保持禁用狀態。有人知道我錯過了什麼?

+0

Rails的是什麼版本的? – Preacher

+0

我覺得2.3.11。不是Rails 3,但這有其原因。 – Ruben

+0

你認爲呢?在控制檯欄中運行-v – bravedick

回答

0

看起來你刪除了這些要求線條:

// This is a manifest file that'll be compiled into including all the files listed below. 
// Add new JavaScript/Coffee code in separate files in this directory and they'll automatically 
// be included in the compiled file accessible from http://example.com/assets/application.js 
// It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the 
// the compiled file. 
// 
//= require jquery 
//= require jquery_ujs 
//= require_tree . 
+0

那麼在我的application.js上面呢?但是我認爲當它在這個文件中時,它不起作用,因爲application.js如何知道它是什麼形式?我從來沒有給過jQuery中的表單名稱? – Ruben

+0

在rails 3中,您在gemfile – bravedick

+0

中默認使用gem'jquery-rails',application.js將在每個頁面上運行此代碼。即使這種形式沒有呈現。不要擔心這個:) – bravedick

0

你寫的JavaScript進去/public/javascripts/application.js。您還需要/ public/javascripts /中的jquery.js或jquery.min.js副本,或者您需要包含一個公共可用版本。確保jQuery在您的application.js之前加載到您的<head>中。

例子:

<%= javascript_include_tag 'https://ajax.googleapis.com/ajax/libs/jquery/1.7.0/jquery.min.js', 'application' %> 
相關問題