开发者

onChange event with rails3 javascript jQuery

开发者 https://www.devze.com 2023-03-25 08:05 出处:网络
I have a voting form where you can also add another answer, and then when you do that only then the voting button should appear...

I have a voting form where you can also add another answer, and then when you do that only then the voting button should appear...

EDIT 2!!!

In my views/index.html.erb i have:

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


            <td>Other:</td>
            <td> 开发者_如何学Python       
              <%= 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 %>

On my application.js file i have:

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

Still the same remains:

Now it works... but if i put some text in one of the text fields all the vote buttons appear. And it only should show the one vote button at that specific answer.

Should i add somehow an incremental id to every text input field and also to the vote buttons?

Anyone? Regards, Thijs


Actually there are few other things as well which needs to be revised in your js code:

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

The solution is very markup dependent and selector might not work as it is as I hove not tested, so try to play around with it a little, if it doesn't.

0

精彩评论

暂无评论...
验证码 换一张
取 消