简体   繁体   English

包括Rails 3中的jQuery

[英]including jquery in rails 3

In my rails application I need to add a jquery effect . 在我的Rails应用程序中,我需要添加一个jquery效果。 A button that should make the content in the paragraph tag hide on clicking the button. 单击该按钮时,应该隐藏段落标记中内容的按钮。 I tried all possible ways but its not working my code for the view 我尝试了所有可能的方法,但是对于视图而言,它不起作用

 <p id = "para1">

  <% javascript_include_tag "tweets" %>
    <%=form_tag({controller: 'tweets', action:'index'},  method: "get") do %>
    <%=label_tag(:search, "Search for:") %>
    <%=text_field_tag(:text)%>
    <%=label_tag(:show, "Show for:") %>
    <%=text_field_tag(:show) %>
    <%= submit_tag( "GO" ) %>

 </p>

  <button type = "button" onclick= "myfunction()">Try it</button>

<%end %>

I have included my jquery code in tweets.js inside app/assets/javascripts folder 我已经在app / assets / javascripts文件夹内的tweets.js中包含了我的jquery代码

<script>
  $(document).ready(function(){
  $("button").click(function(){
    $(#para1).hide();
  });
});
</script>

But in my final view I am getting the search button but when I click nothing is happening .Its not directing to that page which contains that function and becoming invisible.Please help me 但是在我的最终视图中,我得到了搜索按钮,但是当我单击时什么也没有发生。它没有指向包含该功能并变得不可见的页面。请帮助我

This is because, you have added form inside paragraph which is not valid html. 这是因为,您在无效的html段落内添加了form try to validate url with http://validator.w3.org/ 尝试使用http://validator.w3.org/验证网址

Along with this, you have a typo. 除此之外,您还有一个错字。

$(#para1).hide(); needs to be $("#para1").hide(); 需要是$("#para1").hide();

After fixing typo and replacing <p id = "para1"> by <div id="para1"> , it will work. 修正拼写错误并将<p id = "para1">替换为<div id="para1"> ,它将起作用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM