将文本框与下拉菜单相关联

时间:2014-03-08 12:04:03

标签: javascript jquery ajax ruby-on-rails-3 ruby-on-rails-3.1

我正在编写一个程序,它包含一个下拉菜单,它包含几个默认值,如下所示:

<%= f.select :securtiy_question, options_for_select([["Select a question.."],
          ["What is your previous cell number?"], ["What is your fathers name?"], ["write your question"]]),{}, :onchange => 'CheckOther(this);' %>

这个的HTML输出是:

<select id="user_securtiy_question" name="user[securtiy_question]" onchange="CheckOther(this);"><option value="Select a question..">Select a question..</option><option value="What is your prevoius cell number?">What is your prevoius cell number?</option><option value="What is your fathers name?">What is your fathers name?</option><option value="write your question">write your question</option></select>

当我在下拉菜单中点击“编写您的问题”时,我想显示一个文本框。为此,我创建了一个空文本框,显示为无。

<%= f.text_field :own_ques, :style => "display: none;"%>

现在我在我的视图中制作一个JAvascript:

<script type="text/javascript">
  CheckOther = (user_securtiy_question) -> if user_securtiy_question.selectedIndex is user_securtiy_question.options_for_select.length - 1 document.getElementById("user_own_ques").style.display = "inline" else document.getElementById("user_own_ques").style.display = "none" document.getElementById("user_own_ques").value = "" return </script>

但是我仍然无法看到文本框出现在某人身上请提出解决方案......

1 个答案:

答案 0 :(得分:0)

您可以在页面中使用简单的jquery。

$("#user_securtiy_question").change(function(){
  $("#user_own_ques").val('');
  if(this.val()== "write your question"){
    $("#user_own_ques").show();
  }else{
    $("#user_own_ques").hide();
  }
});