簡體   English   中英

Rails:如何將jQuery與HTML分開?

[英]Rails: How to seperate jquery from html?

真的很簡單的問題。

我正在Ruby on Rails中為Redmine編寫插件。

我想做這個

我的form.html.rb中有這個:

<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 %>

我在“ checkbox_enable.js”文件中有此文件:

$(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);
    }
  });
}

看我文件的第一行。 我不知道這是對的。 我不太了解javascript和jquery之間的區別。 但是javascript對html文件沒有影響。

我該如何工作?

編輯:

我只是注意到小提琴僅適用於jquery 1.5。 或似乎是這樣。 這是否意味着很可能無法在大多數(大多數?)瀏覽器上運行? 如果是這樣,為什么有人會使用jquery做任何事情?

請嘗試以下方法。

$( 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();
});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM