简体   繁体   English

在重新加载页面之前,复选框未获得检查状态

[英]Checkbox not getting checked state before page reload

My app has a table of venues and areas, where each area has many venues. 我的应用程序有一个场地和区域的表,每个区域有许多场地。 The index page displays all the venues as partials. 索引页面将所有场地显示为部分。

The index page also has a form containing a checkbox for each area and a submit button and allows the user to filter the venues shown depending on what area(s) are selected. 索引页面还有一个表单,其中包含每个区域的复选框和一个提交按钮,并允许用户根据选择的区域过滤显示的场地。 The checkboxes are using the jQuery-UI button widget to get a better appearance. 复选框使用jQuery-UI按钮小部件来获得更好的外观。

The index page also has a .png map image of a town showing all the differant areas and each area is clickable and corresponds with an area checkbox in the filter form. 索引页面还有一个城镇的.png地图图像,显示所有不同的区域,每个区域都是可点击的,并与过滤器表单中的区域复选框相对应。 The map.png is the background of an image-map containing a poly shape for each area. map.png是包含每个区域的多边形形状的图像映射的背景。

I've managed to link the areas on the map to the area checkboxes using javascript so a user can select either checkboxes in the form or the areas on the map to filter the results. 我已设法使用javascript将地图上的区域链接到区域复选框,因此用户可以选择表单中的复选框或地图上的区域来过滤结果。

$(function() {
  $('area').click(function(){
    var name = $(this).data("areanum");
    var $checkbox = $('#' + name);
    $checkbox.attr('checked', !$checkbox.attr('checked'));
  });       
});

The areas on the map have an areanum value which matches the id of its corresponding checkbox. 地图上的区域具有与其相应复选框的id匹配的areanum值。

However, if the user clicks an area on the map the corresponding checkbox won't look like its been checked until after the form has been submitted and the page reloads. 但是,如果用户单击地图上的某个区域,则在提交表单并重新加载页面之前,相应的复选框将不会被检查。

How can I make it so as soon as an area is selected on the map the corresponding checkbox also appears to have been checked? 如何在地图上选择某个区域后立即进行相应复选框的检查?

If any other code is wanted please ask and I'll post, I didn't want to bog the question down with reams of code as I'm not too sure as what would be important. 如果需要任何其他代码,请询问,我会发布,我不想用大量的代码来解决问题,因为我不太确定什么是重要的。

Any help on this will be very much appreciated! 任何有关这方面的帮助将非常感谢!

... The checkboxes are using the jQuery-UI button widget to get a better appearance. ...复选框使用jQuery-UI按钮小部件来获得更好的外观。 ... ...

Aha! 啊哈! When programmatically changing a checkbox state for custom jQuery UI checkboxes you will need to explicitly tell the checkbox to refresh it's display state: 当以编程方式更改自定义jQuery UI复选框的复选框状态时,您需要明确告诉复选框刷新其显示状态:

$('area').click(function(){
    var name = $(this).data("areanum");
    var $checkbox = $('#' + name);
    $checkbox.attr('checked', !$checkbox.attr('checked'));
    $checkbox.button("refresh");
});  

尝试这个:

$("input[type=checkbox]").prop('checked', true).uniform();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM