I am not sure if this should be done in the Java script or in the HTML part. I am looking to use in the HTML part something like this:
<%= f.hidden_field :skip_option, :value => "the_value_from_the_select_tag" %>
An working solution in the Java Script will be also acceptable.
Here is my code:
<%= simple_form_for(@option) do |f| %>
<%= f.error_notification %>
<%= link_to 'Start assessments', url_for(controller: :assessments, action: :index), id: 'start_assessment' %>
<%= select_tag "skip_option", options_for_select([ "Yes", "No" ], "No"), id: 'skip_option', onchange: "reason_picker()" %>
<%= f.text_field :reason_for_skipping, id: 'reason_for_skipping' %>
<%= f.button :submit, 'Save Option' %>
<% end %>
<script type="text/javascript">
$(document).ready (
window.reason_picker = function () {
var selected = document.getElementById("skip_option").selectedIndex;
if (selected == 0) {
$("#reason_for_skipping").show();
$("#start_assessment").hide();
}
else {
$("#reason_for_skipping").hide();
$("#start_assessment").show();
}
}
);
</script>
Don't know ho to assign the value from the select_tag to the hidden form field.
隐藏字段的行为与其他表单输入一样,因此应该可以:
$("#id_of_hidden_field").val($("#skip_option").val());
Here is the complete working code based on the hint I received. Some changes were required. Some elements were renamed to prevent IDs collision and a new hidden element was introduced. Also in the model class a method to convert empty strings to the database NULL value was used.
This is the file _form.html.erb
<%= simple_form_for(@option) do |f| %>
<%= f.error_notification %>
<div class="form-inputs">
<table>
<thead>
<th>Action</th>
<th>Skip</th>
<th>Reason for skipping</th>
</thead>
<tbody>
<tr>
<td>
<%= link_to 'Start assessments', url_for(controller: :assessments, action: :index), id: 'start_assessment' %>
</td>
<td>
<%= f.hidden_field :skip_option %>
<%= select_tag "html_skip_option", options_for_select([ "Yes", "No" ], :selected => f.object.skip_option), id: 'html_skip_option', onchange: "reason_picker()" %>
</td>
<td>
<%= f.text_field :reason_for_skipping, id: 'reason_for_skipping' %>
</td>
</tr>
</tbody>
</table>
</div>
<div class="form-actions">
<%= f.button :submit, 'Save Option' %>
</div>
<% end %>
<script type="text/javascript">
$(document).ready (
window.reason_picker = function () {
var selected = document.getElementById("html_skip_option").selectedIndex;
if (selected == 0) {
$("#reason_for_skipping").show();
$("#start_assessment").hide();
}
else {
$("#reason_for_skipping").hide();
$("#start_assessment").show();
}
$("#option_skip_option").val($("#html_skip_option").val());
if ($("#option_skip_option").val() != "Yes") {
$("#reason_for_skipping").val("");
}
}
);
</script>
And here is the model file option.rb
class Option < ActiveRecord::Base
before_save :normalize
def normalize
self.reason_for_skipping = nil unless skip_option == "Yes"
end
end
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.