[英]Why the check box in simple_form view always returns 1 when being checked or unchecked in rails 3.2?
A checkbox
(need_delivery) is used to dictate if next 2 text boxes are showing or not. checkbox
(need_delivery)用于指示是否显示下两个文本框。 Those 2 text boxes are wrapped in div with id task_request_delivery
. 这两个文本框包含在id为
task_request_delivery
div中。 Here is the code in simple_form view task_request
: 这是simple_form视图
task_request
的代码:
<%= f.input :need_delivery, :label => 'Need Delivery:', :as => :boolean %>
<div id="task_request_delivery">
<%= f.input :what_to_deliver, :label => 'Content:', :input_html => {:rows => 4} %>
<%= f.input :delivery_instruction, :label => 'Instruction:', :input_html => {:rows => 4} %>
</div>
The corresponding js code under javascript is: javascript下相应的js代码是:
$(function(){
$("#task_request_need_delivery").change(function() {
var rpt = $('#task_request_need_delivery').val();
alert(rpt);
if ( rpt == 1 || rpt == 'true' || rpt == '1') {
$("#task_request_delivery").show();
} else {
$("#task_request_delivery").hide();
}
});
});
The alert(rpt)
is inserted to display the value of rpt
. 插入
alert(rpt)
以显示rpt
的值。 Here is what we found: 这是我们发现的:
rpt
is always 1 when checking or unchecking the checkbox. rpt
的值始终为1。 if ( rpt == 1 || rpt == 'true' || rpt == '1')
always returns false if ( rpt == 1 || rpt == 'true' || rpt == '1')
始终返回false After checking the box initially, the 2 text boxes get hidden. 最初检查框后,隐藏了2个文本框。 After that, unchecking or checking the box did not change anything on the view (2 text boxes still hidden).
之后,取消选中或选中该框并未更改视图上的任何内容(仍隐藏2个文本框)。 Also the
rpt
is always 1
and if ( rpt == 1 || rpt == 'true' || rpt == '1')
is always evaluated as false
and this is contradictory ( rpt
is 1 and then, if
should be true). 此外
rpt
始终为1
且if ( rpt == 1 || rpt == 'true' || rpt == '1')
总是被评估为false
并且这是矛盾的( rpt
是1然后, if
应该是true) )。 What's wrong with the code above? 上面的代码有什么问题? Thanks for the help.
谢谢您的帮助。
我认为你应该使用.is(':checked')
方法来确保你的复选框是否被选中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.