Javascript 提交时是否从rails表单中删除所需属性?
我有一个rails表单,它有一个带有必需属性的单选按钮。我需要“检查”按钮所需的属性。但当有人单击“标记为缺席”按钮时,它将从所有这些单选按钮中删除此必需属性。以下是表单的外观: 这是我的表格。它与rails代码混合在一起,因此不会让您感到困惑。Javascript 提交时是否从rails表单中删除所需属性?,javascript,jquery,html,ruby-on-rails,ruby-on-rails-4,Javascript,Jquery,Html,Ruby On Rails,Ruby On Rails 4,我有一个rails表单,它有一个带有必需属性的单选按钮。我需要“检查”按钮所需的属性。但当有人单击“标记为缺席”按钮时,它将从所有这些单选按钮中删除此必需属性。以下是表单的外观: 这是我的表格。它与rails代码混合在一起,因此不会让您感到困惑。 <table class="listing table table-striped" style="margin-bottom:50px;"> <thead>
<table class="listing table table-striped" style="margin-bottom:50px;">
<thead>
<tr class="header">
<th><span style="color:#F5F5F5;"><%= checkins_counter %>.</span> <%= company.name %></th>
<th colspan="2">
<div style="float:right;">
<% if checkin.checked == true %>
<%= link_to "#",:class => "minus_toggle btn btn-link", :style => "display:none;" ,:id => "minus_#{company.id}" do %>
<span class="glyphicon glyphicon-minus" style="color:#fff"></span>
<% end %>
<%= link_to "#",:class => "plus_toggle btn btn-link", :id => "plus_#{company.id}" do %>
<span class="glyphicon glyphicon-plus" style="color:#fff"></span>
<% end %>
<% else %>
<%= link_to "#",:class => "minus_toggle btn btn-link" ,:id => "minus_#{company.id}" do %>
<span class="glyphicon glyphicon-minus" style="color:#fff"></span>
<% end %>
<%= link_to "#",:class => "plus_toggle btn btn-link", :style => "display:none;" ,:id => "plus_#{company.id}" do %>
<span class="glyphicon glyphicon-plus" style="color:#fff"></span>
<% end %>
<% end %>
<%= hidden_field_tag :week, @week %>
<%= hidden_field_tag :company_id, company.id %>
<%= hidden_field_tag :checkins_size, @checkins.size %>
<%= hidden_field_tag :previous_target_content, previous_target %>
<%= f.submit "Check", :class => "btn btn-success btn-sm" %>
<% if checkin.absent == true %>
<span id="undo_btn_<%= company.id %>">
<%= f.submit "Undo", :class => "btn btn-danger btn-sm" %>
Marked as absent
</span>
<%= f.submit "Mark as absent", :class => "btn btn-danger btn-sm absent_btn", :id => "hihi", :style => "display:none;" %>
<% else %>
<span id="absent_btn_<%= company.id %>" style="display:none;">
<%= f.submit "Undo", :class => "btn btn-danger btn-sm" %>
Marked as absent
</span>
<%= f.submit "Mark as absent", :class => "btn btn-danger btn-sm absent_btn", :id => "absent_btn_#{company.id}" %>
<% end %>
</div>
</th>
</tr>
</thead>
<% if checkin.checked == true %>
<tbody id="tbody_<%= company.id %>" style="display:none;">
<% else %>
<tbody id="tbody_<%= company.id %>">
<% end %>
<tr>
<td>Target</td>
<td>
<b>Previous target: </b><br/>
<% if previous_target != "" && !previous_target.blank? %>
<i><%= previous_target %></i>
<% else %>
N/A
<% end %>
<br/>
<% if @week != "1" %>
<% if checkin.previous_target == false && checkin.checked == false %>
Done <%= f.radio_button :previous_target, 'true', :checked => 'false', :required => true %>
Not Done <%= f.radio_button :previous_target, 'false', :checked => 'false', :required => true %>
<% else %>
Done <%= f.radio_button :previous_target, 'true', :required => true %>
Not Done <%= f.radio_button :previous_target, 'false', :required => true %>
<% end %>
<br/>
<% end %>
<br/>
<b>New target: </b><br/>
<%= f.text_area :target, :class => "form-control", :id => "target_#{company.id}" %>
<br/>
</td>
<td></td>
</tr>
<tr>
<td>Comment</td>
<td><%= f.text_area :comment, :class => "form-control" %></td>
<td></td>
</tr>
<tr>
<td>Points</td>
<td>
1 <%= f.radio_button :point1, '1', :required => true %>
2 <%= f.radio_button :point1, '2', :required => true %>
3 <%= f.radio_button :point1, '3', :required => true %>
4 <%= f.radio_button :point1, '4', :required => true %>
5 <%= f.radio_button :point1, '5', :required => true %>
Execution<br/><br/>
1 <%= f.radio_button :point2, '1', :required => true %>
2 <%= f.radio_button :point2, '2', :required => true %>
3 <%= f.radio_button :point2, '3', :required => true %>
4 <%= f.radio_button :point2, '4', :required => true %>
5 <%= f.radio_button :point2, '5', :required => true %>
Team<br/><br/>
1 <%= f.radio_button :point3, '1', :required => true %>
2 <%= f.radio_button :point3, '2', :required => true %>
3 <%= f.radio_button :point3, '3', :required => true %>
4 <%= f.radio_button :point3, '4', :required => true %>
5 <%= f.radio_button :point3, '5', :required => true %>
Initiative<br/><br/>
1 <%= f.radio_button :point4, '1', :required => true %>
2 <%= f.radio_button :point4, '2', :required => true %>
3 <%= f.radio_button :point4, '3', :required => true %>
4 <%= f.radio_button :point4, '4', :required => true %>
5 <%= f.radio_button :point4, '5', :required => true %>
Prompt communication <br/><br/>
1 <%= f.radio_button :point5, '1', :required => true %>
2 <%= f.radio_button :point5, '2', :required => true %>
3 <%= f.radio_button :point5, '3', :required => true %>
4 <%= f.radio_button :point5, '4', :required => true %>
5 <%= f.radio_button :point5, '5', :required => true %>
Coachability<br/>
</td>
<td></td>
</tr>
</tbody>
</table>
p/s:我需要升级,因为页面上有多个表单,如上图所示 你真的不需要升级。只需在
t车身上应用选择器
$(".absent_btn").click(function()
{
$(this).closest('form').find('tbody input').each(function(){
$(this).removeAttr('required');
});
});
用于切换:
$(".absent_btn").click(function()
{
if($(this).is(":checked"))
{
$(this).closest('form').find('tbody input').each(function(){
$(this).removeAttr('required');
});
}
else
{
$(this).closest('form').find('tbody input').each(function(){
$(this).attr('required','required');
});
}
});
代码是工作:
$(".absent_btn").click(function()
{
$(this).parents("table").find("input[type=radio]").each(function(){
$(this).removeAttr('required');
});
});
我不知道为什么。removeAttr不适合我。我试图检查它,但浏览器给了我“uncaughtsyntaxerror:Unexpected-token-liked”错误,这根本没有用 无论如何,这是最终对我有效的解决方案:
$(".absent_btn").click(function()
{
$(this).parents('table').find('tbody input').prop('required', false);
});
嗯,我需要升级,因为我在页面上有多个表单,它们的输入名称和所有内容都相同。请看我问题中的更新照片。我如何使用您的解决方案来解决此问题?谢谢对于多个表单,只需使用父(“表单”)作为主选择器。我已经更新了解决方案中的代码。嗯,我已经尝试了您的解决方案,但是表单不断弹出“请选择此选项之一”。这是因为我使用的是rails表单还是别的什么?只要它们在解析后变成html,那么使用哪种语言没有任何区别。元素是否以相同的形式或其他形式弹出“请选择此选项之一”?它的形式相同选择器为.parent(),而不是parents(),Hm看起来很有希望,但不知何故这不起作用..表单不断弹出“请选择此选项之一”首先,jQuery有parents()方法,除非您不使用jQuery。然后,如果不工作,您可以看到F12上的错误并找出错误所在。
$(".absent_btn").click(function()
{
$(this).parents('table').find('tbody input').prop('required', false);
});