【问题标题】:Checkbox unchecked after refresh刷新后取消选中复选框
【发布时间】:2015-04-18 20:29:11
【问题描述】:

我的页面上有复选框,所有复选框的默认值为 true。但是,select_all 复选框在页面刷新后变为未选中状态,而其他复选框保持选中状态。

<script>
	var $checked = true;
	$('#select_all').click(function() {
		console.log('click select all button'+$checked)
		$checked = !$checked
		$(".activated").each(function(){
			$(this).prop("checked", $checked)
		});
	});
</script>
<table cellspacing="10">
	    <thead>
		    <tr>
					<%= check_box_tag 'select_all', 1, true%>
					<%= label_tag :select_all, " Select All Doctors" %>
				</tr>
	   </thead>
	   <tbody>
		   <tr>
				 <th>Send Email</th>
			   <th>First Name</th>
			   <th>Last Name</th>
			   <th>Email</th>
		   <% @doctors.each do |doctor| %>
			 <tr>
         <td><%= check_box_tag 'activated[]', doctor.id, true, class: 'activated'%></td>
				 <td><%= doctor.first_name %></td>
				 <td><%= doctor.last_name %></td>
				 <td><%= doctor.email %></td>
			 </tr>
		   <% end %>
	   </tbody>
</table>

【问题讨论】:

标签: javascript checkbox erb


【解决方案1】:

您不必将布尔值保存为 toogle,您可以执行以下操作:

<script>
    $('#select_all').change(function() {
      console.log('click select all button'+$checked)
      $checked = $(this).is(':checked')
      $(".activated").each(function(){
        $(this).prop("checked", $checked)
      });
    });
</script>

N.T 将点击替换为更改,它适合与复选框一起使用。

【讨论】:

    猜你喜欢
    • 2021-05-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-24
    • 2016-08-01
    • 2019-01-14
    相关资源
    最近更新 更多