onChange event with rails3 javascript jQuery



我有一个投票表单,您可以在其中添加另一个答案,然后当您这样做时,投票按钮应该出现…

编辑2 ! !

In my views/index.html。动词I have:

<table>
<% @questions.each do |question| %>
<% for answer in question.answers %>

            <td>Other:</td>
            <td>        
              <%= form_tag('/vote/new_answer', :method => "post") do %>
              <%= hidden_field_tag('answer[question_id]', question.id) %>
              <%= hidden_field_tag('answer[user_id]', current_user.id) %>
            <div class="other_answer">
              <%= text_field_tag('answer[content]') %>
            </div>
          </td>
            <td>
                <div class="other_answer_button", hidden='true'>
              <%= submit_tag('Vote') %>
                </div>
<% end %>
<% end %>

在我的application.js文件我有:

$(function() {
    $(".other_answer input").change(function() {
        if ($.trim($(this).val()) == "") {
            $(".other_answer_button").hide();
        }
        else {
            $(".other_answer_button").show();
       }       
    });
});

仍然是相同的:

现在它工作了…但是如果我在其中一个文本字段中放入一些文本,所有的投票按钮就会出现。它只应该在那个特定的答案上显示一个投票按钮。

我应该以某种方式添加一个增量id到每个文本输入字段,也投票按钮?

有人知道吗?问候,Thijs

实际上还有一些其他的东西需要在你的js代码中修改:

$(function() {
    $(".other_answer input").change(function() {
        if ($.trim($(this).val()) == "") {
            $("input", $(this).parents("td").next()).hide();
        }
        else {
            $("input", $(this).parents("td").next()).hide();
       }       
    });
});

解决方案是非常依赖于标记和选择器可能不工作,因为它是我没有测试过,所以试着玩一下,如果它没有。

最新更新