简体   繁体   English

Javascript代码无法在Rails中正常工作

[英]Javascript code not working properly in rails

index.html.erb index.html.erb

<div id="app-back-button">
  <%= link_to image_tag("back.png",:border => 0), 'javascript:history.go(-1);' %>
</div>

Above mentioned code triggers back action when back button is clicked. 上面提到的代码在单击后退按钮时触发后退动作。

index.html.erb index.html.erb

Below mentioned code is used to hide and show the div contents. 下面提到的代码用于隐藏和显示div内容。

<a href="#">LINK</a>

<div id = "test">

    <h2>Import Statements</h2>

    <%= form_tag import_xvaziris_path, multipart: true do %>
    <%= file_field_tag :file %>
    <%= submit_tag "Import" %>
    <% end %>

</div>


<script language="javascript" type="text/javascript">
$( document ).ready(function() {
    $("div#test").hide();
    $("a").click(function(event) {
        event.preventDefault();
        $("div#test").toggle();
    });
});
</script>

Now the problem is that when I click the back button, it triggers hide/show functionality and do not go backwards. 现在的问题是,当我单击“后退”按钮时,它会触发隐藏/显示功能,并且不会向后退。

Any suggestions are most welcome. 任何建议都是最欢迎的。

Thank you in advance. 先感谢您。

Full index view is as below; 完整索引视图如下所示;

index.html.erb index.html.erb

<div id="content_header">
    <h1 id="main_heading">XTEP Vaziri Statement</h1>

    <div id="app-back-button">
        <%= link_to image_tag("back.png",:border => 0), 'javascript:history.go(-1);' %>
    </div>

</div>

<br>

<a href="#">...</a>

<div id = "test">

    <h2>Import Statements</h2>

    <%= form_tag import_xvaziris_path, multipart: true do %>
    <%= file_field_tag :file %>
    <%= submit_tag "Import" %>
    <% end %>

</div>


<script language="javascript" type="text/javascript">
$( document ).ready(function() {
    $("div#test").hide();
    $("a").click(function(event) {
        event.preventDefault();
        $("div#test").toggle();
    });
});
</script>

@WesFoster has explained you the reason. @WesFoster向您解释了原因。 But, if you remove the preventDefault() , it would still be "toggling" your div . 但是,如果删除了preventDefault() ,它仍将“切换” div Even though your page goes back. 即使您的页面返回。 That's because you are selecting ALL anchor tags on your page. 那是因为您要在页面上选择所有锚标记。

A better way would be to select specific toggle link and add toggle functionality to that specific element. 更好的方法是选择特定的切换链接并将切换功能添加到该特定元素。

 $(document).ready(function() { $("div#test").hide(); $("a.form-toggle-link").click(function(event) { event.preventDefault(); $("div#test").toggle(); }); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <a href="#" class="form-toggle-link">LINK</a> <div id="test"> <h2>Import Statements</h2> ... </div> <div id="app-back-button"> <a href="javascript:history.go(-1);">Another link</a> </div> 


Full index.html.erb: 完整的index.html.erb:

<div id="content_header">
  <h1 id="main_heading">XTEP Vaziri Statement</h1>
  <div id="app-back-button">
    <%=link_to image_tag("back.png", :border=>0), 'javascript:history.go(-1);' %>
  </div>
</div>

<br>

<!--           |=== HERE  -->
<a href="#" class="toggle-form">...</a>

<div id="test">
  <h2>Import Statements</h2>
  <%=f orm_tag import_xvaziris_path, multipart: true do %>
    <%=f ile_field_tag :file %>
    <%=s ubmit_tag "Import" %>
  <% end %>
</div>


<script language="javascript" type="text/javascript">
  $(document).ready(function() {
    $("div#test").hide();

    //    | === HERE
    $("a.toggle-form").click(function(event) {
      event.preventDefault();
      $("div#test").toggle();
    });
  });
</script>

It's not going "back" because you have prevented it from doing so: 它不会“返回”,因为您已阻止它这样做:

# This prevents the link from executing
event.preventDefault();

Therefore, the javascript:history.go(-1); 因此, javascript:history.go(-1); is not executing. 没有执行。

Remove the preventDefault() and it will allow you to go "back" as intended. 删除preventDefault() ,它将使您可以按预期“返回”。

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

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