我在我的 rails 应用程序中的一个名为 custom.js 的文件中编写了以下 jquery。我写这个是为了在单击复选框时打开一个文本框。
$(document).on('page:load', function(){
$(".category_check").on("change", function(){
//alert("test")
// alert()
//alert($(this).data('category'))
$("#" + $(this).data('category')).toggle();
});
$(".quality_check").on("change", function(){
//alert("test")
// alert()
//alert($(this).data('category'))
$("#" + $(this).data('quality')).toggle();
});
$(".duration_check").on("change", function(){
//alert("test")
// alert()
//alert($(this).data('category'))
$("#" + $(this).data('duration')).toggle();
});
$(".delivery_time_check").on("change", function(){
//alert("test")
// alert()
//alert($(this).data('category'))
$("#" + $(this).data('delivery_time')).toggle();
});
});
这是代码的html部分:
<label>Categories</label>
<% ClipCategory.all.each do |category| %>
<%= form_tag "update_amount", :remote => :true do %>
<div class="span12">
<div class="category_check pull-left" data-category="category_text_<%= category.id %>">
<%= check_box_tag "category", 1, ( @performer.clip_category_ids.include?(category.id) ? "checked" : false ), :class => "pull-left" %>
<%= label_tag "category", category.name, :class => "pull-left category_label" %>
</div>
<% amount_val = ClipCategoryPerformer.where("clip_category_id = ? AND performer_id = ?", category.id, current_user.performer.id ).first %>
<div id="category_text_<%= category.id%>" class="category_text span10 pull-right">
<% if amount_val %>
<%= number_field_tag "amount", @amount, :class => "pull-left input-medium", :value => amount_val.amount %>
<% else %>
<%= number_field_tag "amount", @amount, :class => "pull-left input-medium", :value => nil %>
<% end %>
<%= hidden_field_tag "category_id", category.id %>
<%= submit_tag "Update" %>
</div>
</div>
<br />
<% end %>
<% end %>
</div>
<div class="tab-pane" id="tab4">
<% Quality.all.each do |quality| %>
<%= form_tag "update_quality_amount", :remote => :true do %>
<div class="span12">
<div class="quality_check pull-left" data-quality="quality_text_<%= quality.id %>">
<%= check_box_tag "quality", 1, ( @performer.quality_ids.include?(quality.id) ? "checked" : false ), :class => "pull-left" %>
<%= label_tag "quality", quality.name, :class => "pull-left category_label" %>
</div>
<% amount_val = ClipCategoryPerformer.where("clip_category_id = ? AND performer_id = ?", quality.id, current_user.performer.id ).first %>
<div id="quality_text_<%= quality.id %>" class="quality_text span10 pull-right">
<% if amount_val %>
<%= number_field_tag "amount", @amount, :class => "pull-left input-medium", :value => amount_val.amount %>
<% else %>
<%= number_field_tag "amount", @amount, :class => "pull-left input-medium", :value => nil %>
<% end %>
<%= hidden_field_tag "quality_id", quality.id %>
<%= submit_tag "Update" %>
</div>
</div>
<br />
<% end %>
<% end %>
</div>
<div class="tab-pane" id="tab5">
<% Duration.all.each do |duration| %>
<%= form_tag "update_duration_amount", :remote => :true do %>
<div class="span12">
<div class="duration_check pull-left" data-duration="duration_text_<%= duration.id %>">
<%= check_box_tag "duration", 1, ( @performer.duration_ids.include?(duration.id) ? "checked" : false ), :class => "pull-left" %>
<%= label_tag "duration", duration.time, :class => "pull-left duration_label" %>
</div>
<% amount_val = ClipCategoryPerformer.where("clip_category_id = ? AND performer_id = ?", duration.id, current_user.performer.id ).first %>
<div id="duration_text_<%= duration.id %>" class="duration_text span10 pull-right">
<% if amount_val %>
<%= number_field_tag "amount", @amount, :class => "pull-left input-medium", :value => amount_val.amount %>
<% else %>
<%= number_field_tag "amount", @amount, :class => "pull-left input-medium", :value => nil %>
<% end %>
<%= hidden_field_tag "duration_id", duration.id %>
<%= submit_tag "Update" %>
</div>
</div>
<br />
<% end %>
<% end %>
</div>
<div class="tab-pane" id="tab6">
<% DeliveryTime.all.each do |delivery_time| %>
<%= form_tag "update_delivery_time_amount", :remote => :true do %>
<div class="span12">
<div class="delivery_time_check pull-left" data-delivery_time="delivery_time_text_<%= delivery_time.id %>">
<%= check_box_tag "delivery_time", 1, ( @performer.delivery_time_ids.include?(delivery_time.id) ? "checked" : false ), :class => "pull-left" %>
<%= label_tag "delivery_time", delivery_time.duration, :class => "pull-left delivery_time_label" %>
</div>
<% amount_val = ClipCategoryPerformer.where("clip_category_id = ? AND performer_id = ?", delivery_time.id, current_user.performer.id ).first %>
<div id="delivery_time_text_<%= delivery_time.id %>" class="delivery_time_text span10 pull-right">
<% if amount_val %>
<%= number_field_tag "amount", @amount, :class => "pull-left input-medium", :value => amount_val.amount %>
<% else %>
<%= number_field_tag "amount", @amount, :class => "pull-left input-medium", :value => nil %>
<% end %>
<%= hidden_field_tag "delivery_time_id", delivery_time.id %>
<%= submit_tag "Update" %>
</div>
</div>
<br />
<% end %>
<% end %>
</div>
当我单击标题中除第一个以外的任何复选框而不是复选框本身时,第一个被选中,而不是我单击的那个。如果我刷新页面,复选框会保留,当我取消选中它时,只有文本框会切换,当我选中它时,它就会消失。我究竟做错了什么?