2016-11-09 5 views
0

本当に簡単な質問です。Rails:htmlからjqueryを分離するには?

私はRuby on RailsでRedmineのプラグインを作成しています。私の "checkbox_enable.js" ファイルで、私はこれを持って

<table> 
    <tr> 
     <td >Specific operations/procedures</td> 
     <td> 
      <input type="checkbox" value="mfi_nam9" class="checkme"/>Other(please specify)<br/> 
      <input type="text" name="mfi_nam9" disabled="true" > 
     </td> 
    </tr> 
    <tr> 
     <td >General principles/strategies</td> 
     <td> 
      <input type="checkbox" value="mfi_nam8" class="checkme"/>Other(please specify):<br/> 
      <input type="text" name="mfi_nam8" disabled="true"> 
     </td> 
    </tr> 
</table> 

# compatibility for use as redmine plugin 
<% content_for :header_tags do %> 
    <%= javascript_include_tag 'checkbox_enable', :plugin => 'students' %> 
<% end %> 

$(function() { 
    $('.checkme').attr('checked', true); 
    $('.checkme').click(function(){ 
    if($('input[name='+ $(this).attr('value')+']').attr('disabled') == false){ 
     $('input[name='+ $(this).attr('value')+']').attr('disabled', true); 
    } else { 
     $('input[name='+ $(this).attr('value')+']').attr('disabled', false); 
    } 
    }); 
} 

は、私は私のform.html.rbでこれを持ってthis

をしたいですそのファイルの私の最初の行を見てください。それが正しいかどうかわかりません。私は実際によくjavascriptとjqueryの違いを理解していない。しかし、javascriptはhtmlファイルに影響を与えません。

この作品を作成するにはどうすればよいですか?

EDIT:

私はバイオリンだけのjQuery 1.5のために働くことに気づきました。それともそうだ。つまり、多くのブラウザ(ほとんどのブラウザ)でうまく動作しない可能性がありますか?もしそうなら、誰もjqueryを何に使うのですか?

+0

jsファイルにスクリプトリンクを追加しましたか?エラーはありますか? – Tinsten

答えて

0

次のようにしてください。

$(document).ready(function() { 
    $('.checkme').click(function(){ 
     // is this checkbox checked 
     // ~~~~~ 
     var bChecked = $(this).is(':checked'); 

     // enable/disable text box based on the checkbox value 
     // ~~~~~ 
     $('input[name='+ $(this).attr('value')+']').attr('disabled', !bChecked); 
    }); 

    // trigger click event on page ready 
    // OR you could remove this line and change the defaults in your 
    // table definition to have the checkboxes checked and the inputs enabled 
    // by default! 
    // ~~~~~ 
    $('.checkme').click(); 
}); 
関連する問題