简体   繁体   English

JavaScript 在页面重新加载 [Rails] 时不起作用?

[英]JavaScript does not work on page reload [Rails]?

This is my JavaScript:这是我的 JavaScript:

function showQuestion(id) {
  $(".question").hide();
  $("#"+id).show();
}

$(document).ready(function() {
  current_question=1;
  showQuestion(current_question);
  $("#next_question").click(function(){
    alert('The current question is '+current_question);
    current_question=current_question+1;
    showQuestion(current_question);
  });
  $("#prev_question").click(function(){
    alert('The current question is '+current_question);
    current_question=current_question-1;
    showQuestion(current_question);
  });
});

And this is my show.html.erb [my Rails view]:这是我的show.html.erb [我的 Rails 视图]:

<div id="questions">
  <% @questions.each_with_index do |q,i| %>
    <div id="<%= i + 1 %>" class="question">
    <strong><%= q.body %></strong>
    <% if q.type == "MultipleChoice" %>
      <% q.choices.each do |choice| %>
        <br /><%= radio_button_tag "question#{q.id.to_s}", "choice#{choice.id}", @answer == choice  %>&nbsp;<%= choice.body %>
      <% end %>
    <% end %>
    <table>
    <tr><td><%= link_to "< Previous", '#', :remote => true, :id => "prev_question" %></td><td><%= link_to "Next >", '#', :remote => true, :id => "next_question" %></td></tr>
    </table>
    </div>
  <% end %>
</div>

So, when I click the 'Next' or 'Previous' button, it works.因此,当我单击“下一个”或“上一个”按钮时,它会起作用。 But after that, neither button works.但在那之后,两个按钮都不起作用。 I have a feeling it has something to do with jQuery not working.我感觉这与 jQuery 无法正常工作有关。 Thanks!谢谢!

If I'm understanding the code correctly (I don't know ruby), your IDs should be unique.如果我正确理解了代码(我不知道 ruby),那么您的 ID 应该是唯一的。 You are creating many buttons all with the same ID.您正在创建许多具有相同 ID 的按钮。 jQuery is only attaching to the first (since there should only be one). jQuery 仅附加到第一个(因为应该只有一个)。

Try changing it to a class instead.尝试将其更改为 class。 Or (probably better), remove the buttons from the loop and only create one set of buttons.或者(可能更好),从循环中删除按钮,只创建一组按钮。

EDIT: Also, while this is probably not causing the issue, IDs should not start with a digit, unless you're using HTML5 which I think may allow this.编辑:另外,虽然这可能不会导致问题,但 ID 不应以数字开头,除非您使用 HTML5,我认为这可能允许这样做。

You are really close!你真的很亲近!

The problem is that you are generating a set of links for each question with the same IDs, which isn't correct.问题是您正在为每个具有相同 ID 的问题生成一组链接,这是不正确的。 It sounds like the first set of links is being hidden when you click one of them the first time.当您第一次单击其中一个时,听起来第一组链接被隐藏了。

One solution is to move the table with the Previous and Next links outside of the tag that contains it.一种解决方案是将带有“上一个”和“下一个”链接的表格移到包含它的标签之外。 Here's a fiddle showing a dumbed-down version of your code with prev and next links outside of the question div.这是一个小提琴,显示了您的代码的简化版本,其中包含问题 div 之外的 prev 和 next 链接。 http://jsfiddle.net/QNLF9/ http://jsfiddle.net/QNLF9/

You could also leave the links inside the question div, assign them a class and use a class selector in your jQuery code.您也可以将链接保留在问题 div 中,为其分配 class 并在 jQuery 代码中使用 class 选择器。 Here's an example.这是一个例子。 http://jsfiddle.net/traDx/ http://jsfiddle.net/traDx/

I hope that helps.我希望这会有所帮助。 One other little thing.还有一件小事。 You should use html escape codes &lt;您应该使用 html 转义码&lt; and &gt;&gt; instead of < and > respectively.而不是<>分别。 Happy coding!快乐编码!

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

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