onChange 事件与rails3 javascript jQuery
我有一个投票表,您还可以在其中添加另一个答案,然后当您这样做时,才会出现投票按钮...
编辑2!
在我的views/index.html.erb中,我有:
<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?
有人吗? 问候, 泰斯
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>
<%= 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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
实际上,还有一些其他事情需要在您的 js 代码中进行修改:
该解决方案非常依赖于标记,并且选择器可能无法正常工作,因为我没有测试过,所以尝试稍微尝试一下,如果它没有。
Actually there are few other things as well which needs to be revised in your js code:
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.