Rails Simple Form Bootstrap-显示输入的Javascript if语句

Rails Simple Form Bootstrap - Javascript if statement to reveal input

本文关键字:Javascript if 语句 输入 显示 Simple Form Bootstrap- Rails      更新时间:2023-09-26

我有一个rails 4应用程序,具有简单的表单和引导程序。

我想在表格中提出一个问题,询问用户是否需要调查回复。如果答案是真的,那么我想问一个后续问题。我的两个调查问题是:

<%= f.input :survey, :as => :boolean, :label => false, inline_label: 'Do you need survey responses?'  %>
<br><br>

    <%= f.input :survey_link, label: 'Where is your survey?', :label_html => { :class => 'question-data' }, placeholder: 'Include a link to your survey', :input_html => {:style=> 'width: 650px; margin-top: 20px',  class: 'response-project'} %>

JS if语句是处理此任务的最佳方式吗?我想隐藏第二个问题,直到用户对第一个问题的回答为真。

如果是这样的话,我很难理解如何使这个JS工作。

我试过:

 if (:survey is :true) {
    :survey_link;
}

我找不到任何资源来帮助解释如何做到这一点。非常感谢您的帮助。非常感谢。

显然,下面的代码取决于实际HTML中surveysurvey_link输入的名称。因此,如果在本例中是针对@user:

<% simple_form_for @user do |f| %>
    <%= f.input :survey, :as => :boolean, :label => false, inline_label: 'Do you need survey responses?'  %>
    <%= f.input :survey_link, label: 'Where is your survey?', :label_html => { :class => 'question-data' }, placeholder: 'Include a link to your survey', :input_html => {:style=> 'width: 650px; margin-top: 20px',  class: 'response-project'} %>
<% end %>

然后JQuery应该看起来像(注意user部分-您可能需要更改它):

<script>
$(function() {
    // Hide the survey_link input
    $('input[name="user[survey_link]"]').hide();
    $('input[name="user[survey_link]"]').closest("label").hide();
    // When the survey checkbox changes
    $('input[name="user[survey]"]').change(function() {
         $('input[name="user[survey_link]"]').closest("label").toggle(this.checked);
         $('input[name="user[survey_link]"]').toggle(this.checked);
    });
});
</script>