如何在页面重新加载时保持imagemap的高亮状态?

时间:2012-01-19 23:06:11

标签: javascript jquery ruby-on-rails persistence imagemap

在我的应用程序中,我将图像映射分成几个多边形。每个形状都有一个相应的复选框(坐在带有提交按钮的表单中),它们允许用户根据选择的图像映射区域过滤显示的结果。

我使用dumbFormState插件在提交表单并重新加载页面之后保留复选框,但是imagemap中的选定形状会丢失其突出显示的状态。

如何让形状保持“已检查”状态,就像重新加载页面上的复选框一样?

这是我的javascript:

<script>
  jQuery(function(){ // ensure dom is loaded first
    jQuery('#filter_form').dumbFormState();
  });  

  $(function() {
    $('.map').maphilight();  

    $('#north, #east, #south, #west').click(function(e) {
        e.preventDefault();
        var $this = $(this);
        var data = $this.mouseout().data('maphilight') || {};
        data.alwaysOn = !data.alwaysOn;
        $this.data('maphilight', data).trigger('alwaysOn.maphilight');
    });
  });

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

index.html.erb

<div class="map_container">

  <%= image_tag("maps/mainmap.png", :width => "450", :height => "450", :class => "map", :usemap => "#mainmap", :alt => "") %>

  <map name="mainmap">
    <area id="north" data-areanum="area-42" shape="poly" 
      coords="158,43,152,49,164,86,165,112,153,153,139,169,145,171,161,176,236,201,241,202,251,166,253,142,257,132,294,102,269,85,240,68,227,53,213,28,202,27" alt="North"
      data-maphilight='{"stroke":false,"fillColor":"5F9EA0","fillOpacity":0.6}'
      onmouseover="document.body.style.cursor='pointer'" 
      onmouseout="document.body.style.cursor='default'" >

    <area id="east" data-areanum="area-44" shape="poly" 
      coords="296,103,258,133,254,143,252,166,242,203,263,209,272,204,322,226,340,250,360,241,356,230,357,222,378,214,395,195,394,188" alt=""
      data-maphilight='{"stroke":false,"fillColor":"5F9EA0","fillOpacity":0.6}'
      onmouseover="document.body.style.cursor='pointer'" 
      onmouseout="document.body.style.cursor='default'" >


    <area id="south" data-areanum="area-41" shape="poly" 
      coords="182,316,178,321,188,329,200,353,276,274,243,248" alt=""
      data-maphilight='{"stroke":false,"fillColor":"5F9EA0","fillOpacity":0.6}'
      onmouseover="document.body.style.cursor='pointer'" 
      onmouseout="document.body.style.cursor='default'" >


    <area id="west" data-areanum="area-48" shape="poly" 
      coords="286,276,276,275,200,354,218,390,323,341,325,321,321,312" alt=""
      data-maphilight='{"stroke":false,"fillColor":"5F9EA0","fillOpacity":0.6}'
      onmouseover="document.body.style.cursor='pointer'" 
      onmouseout="document.body.style.cursor='default'" >
  </map>              
</div>

<div class="filter_options_container">
  <%= form_tag '', :method => :get, :id => 'filter_form' do %>

    <fieldset class="filter_form_fieldset areas">
      <% Area.all.each do |a| %>
        <p class="area_check"><%= check_box_tag 'areas[]', a.id, false, :id => "area-#{a.id}" %>
        <label for="area-<%= a.id %>"><p1><%= a.name %></p1></label></p>
      <% end %>
    </fieldset>

    <div class="filter_form_button">
      <p2><input type="submit" value="Show me"/></p2>
    </div>
  <% end %>
</div>

浏览器中生成的内容

<div class="map_container">

  <img alt="" class="map" height="450" src="/assets/maps/mainmap.png" usemap="#mainmap" width="450" />

  <map name="mainmap">
    <area id="north" data-areanum="area-42" shape="poly" 
      coords="158,43,152,49,164,86,165,112,153,153,139,169,145,171,161,176,236,201,241,202,251,166,253,142,257,132,294,102,269,85,240,68,227,53,213,28,202,27" alt="North"
      data-maphilight='{"stroke":false,"fillColor":"5F9EA0","fillOpacity":0.6}'
      onmouseover="document.body.style.cursor='pointer'" 
      onmouseout="document.body.style.cursor='default'" >

    <area id="east" data-areanum="area-44" shape="poly" 
      coords="296,103,258,133,254,143,252,166,242,203,263,209,272,204,322,226,340,250,360,241,356,230,357,222,378,214,395,195,394,188" alt=""
      data-maphilight='{"stroke":false,"fillColor":"5F9EA0","fillOpacity":0.6}'
      onmouseover="document.body.style.cursor='pointer'" 
      onmouseout="document.body.style.cursor='default'" >

    <area id="south" data-areanum="area-41" shape="poly" 
      coords="182,316,178,321,188,329,200,353,276,274,243,248" alt=""
      data-maphilight='{"stroke":false,"fillColor":"5F9EA0","fillOpacity":0.6}'
      onmouseover="document.body.style.cursor='pointer'" 
      onmouseout="document.body.style.cursor='default'" >


    <area id="west" data-areanum="area-48" shape="poly" 
      coords="286,276,276,275,200,354,218,390,323,341,325,321,321,312" alt=""
      data-maphilight='{"stroke":false,"fillColor":"5F9EA0","fillOpacity":0.6}'
      onmouseover="document.body.style.cursor='pointer'" 
      onmouseout="document.body.style.cursor='default'" >        
  </map>              
</div>

<div class="filter_options_container">
  <form accept-charset="UTF-8" action="" id="filter_form" method="get"><div style="margin:0;padding:0;display:inline"><input name="utf8" type="hidden" value="&#x2713;" /></div>    

    <fieldset class="filter_form_fieldset areas">
        <p class="area_check"><input id="area-41" name="areas[]" type="checkbox" value="41" />
        <label for="area-41"><p1>south</p1></label></p>    
        <p class="area_check"><input id="area-42" name="areas[]" type="checkbox" value="42" />
        <label for="area-42"><p1>north</p1></label></p>
        <p class="area_check"><input id="area-44" name="areas[]" type="checkbox" value="44" />
        <label for="area-44"><p1>east</p1></label></p>
        <p class="area_check"><input id="area-48" name="areas[]" type="checkbox" value="48" />
        <label for="area-48"><p1>west</p1></label></p>

    </fieldset>

    <div class="filter_form_button">
      <p2><input type="submit" value="Show me"/></p2>
    </div>
</form></div>

如果我遗漏了任何问题,请发表评论。

感谢任何帮助,非常感谢!

1 个答案:

答案 0 :(得分:0)

您可能希望序列化数据并将其保存到window.onbeforeunload事件中的Cookie或LocalStorage。 在document.onload中,您应该检查存储的数据,如果存在,则应用然后删除它们。