我有一些图像映射区域和一些复选框。每个区域都有一个相应的复选框,如何在单击某个区域时勾选相应的复选框?
图像映射:
<%= image_tag("maps/mainmap.png", :width => "450", :height => "450", :class => "map", :usemap => "#mainmap", :alt => "") %>
<map name="mainmap">
<area id="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="area-43" 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'" >
</map>
复选框:
<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>
这产生了这个:
<img alt="" class="map" height="450" src="/assets/maps/mainmap.png" usemap="#mainmap" width="450" />
<map name="mainmap">
<area id="area-41" 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="area-42" 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'" >
</map>
<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>Chinatown</p1></label></p>
<p class="area_check"><input id="area-42" name="areas[]" type="checkbox" value="42" />
<label for="area-48"><p1>Village</p1></label></p>
</fieldset>
我认为这个javascript可能会将这两者联系起来,但它不起作用,关于我哪里出错的任何想法?
$(function() {
$('area').click(function(){
var name = $(this).attr('id');
var $checkbox = $('[id=' + id + ']');
$checkbox.attr('checked', !$checkbox.attr('checked'));
});
});
非常感谢您的帮助!
答案 0 :(得分:3)
您在<area>
标记和复选框中使用了相同的ID值。那永远不会奏效。您在获取$checkbox
变量方面也遇到了一些错误。
我更改了<area>
标记以使用数据值并更改了代码以检索该数据值,现在它可以正常工作。你可以在这里看到它:http://jsfiddle.net/jfriend00/ndwjC/。
由于我们没有实际图像,因此必须猜测点击查找热点的位置,但是当您找到热点时,它会切换一个复选框。
HTML:
<img alt="" class="map" height="450" src="/assets/maps/mainmap.png" usemap="#mainmap" width="450" />
<map name="mainmap">
<area data-areanum="area-41" 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 data-areanum="area-42" 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'" >
</map>
<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>Chinatown</p1></label></p>
<p class="area_check"><input id="area-42" name="areas[]" type="checkbox" value="42" />
<label for="area-48"><p1>Village</p1></label></p>
</fieldset>
使用Javascript:
$(function() {
$('area').click(function(){
var name = $(this).data("areanum");
var $checkbox = $('#' + name);
$checkbox.attr('checked', !$checkbox.attr('checked'));
});
});
P.S。如果您使用的是最新版本的jQuery,则应该使用.prop()
更改复选框而不是.attr()
。