gpt4 book ai didi

javascript - 获取传单层控制复选框的状态

转载 作者:行者123 更新时间:2023-12-04 17:48:44 24 4
gpt4 key购买 nike

我有几个函数使用 Leaflet 在 map 上绘制不同的东西,其中一个有一个图层控件,可以显示/隐藏 map 上的一些扇区。另一个函数绘制电梯(直线)。

根据用户操作, map 上显示的内容会发生变化,我会重新绘制电梯。
我希望仅在用户选中复选框时才绘制扇区,但我不知道如何获取复选框的值并将其传递给提升功能(如果用户选中了该功能,则应触发扇区功能)复选框)。

如何保存图层控件复选框的值并在另一个 Ajax 函数(提升)中测试它?

$('#build').on("click", function(e){
$.ajax({
type: "POST",
url: map_controller/show_lift_types",
success: function(result){
if (result.returned == true){
// ... Displays some information in the page
drawLift(); // Draws the lifts
// If the user has chosen to show the sector layer, need to call drawSectors
drawSectors();
}
}
});
});


function drawLift() {
if (typeof lift_path !== 'undefined') { // If lift_path (the lifts) exists
map.eachLayer(function (layer) { // For each layer
console.log(layer._leaflet_id);
if (typeof layer._path !== 'undefined') { // Only if the _path variable exist. Excludes the background image of the map and already built lift
map.removeLayer(layer); // Remove the layer
}
});
}

$.ajax({
type: "POST",
dataType: "json",
url: map_controller/get_lifts_map",
success: function(result){
for ( i=0; i < result.id_group.length; i++ ) {
// ... retrieves parameters ...
var path_info = {
"type": "Feature",
"features": [{
"type": "Feature",
"geometry": {
"type": "LineString",
"coordinates": [start_location, end_location]
}
}]
};
lift_path = new L.geoJson(path_info,style: style}).on('click', function (e) {
// ... Some function...
}).addTo(map);
}
}
});
};

function drawSector() {
var sector_path = new Array()
var baseLayers;
$.ajax({
type: "POST",
dataType: "json",
url: map_controller/get_sectors_map",
success: function(result){
for ( i=0; i < result.path.length; i++ ) {
// ... retrieves parameters ...
var sectors = {
"type": "Feature",
"geometry": {
"type": "Polygon",
"coordinates": path
}
};
sector_path[i] = L.geoJson(sectors, style);
}
var sectors = L.layerGroup([sector_path[0], sector_path[1], sector_path[2]]).addTo(map);
var overlays = {};
overlays[Settings.show_sectors] = sectors; // Show sector checkbox
L.control.layers(baseLayers, overlays).addTo(map);
}
});
}

// do the actual ajax calls
drawSector();
drawLift();

更新:根据@davojta 的建议,这是我的完整解决方案:
$(document).on('change', '.leaflet-control-layers-selector', function() {
$checkbox = $(this);
if ($checkbox.is(':checked')) {
sectorLayerCheckbox = true;
}
else {
sectorLayerCheckbox = false;
}
}

drawLift我补充说:
if (typeof sectorLayerCheckbox == 'undefined' || sectorLayerCheckbox != false) {  
drawSector();
}

最佳答案

一般算法可能如下

  • 使用数据属性将一些元数据添加到您的复选框中
    <input id="checkBox" type="checkbox" data-lyftFlag="flagId">
  • 听更改发明并在选中复选框后采取行动
    $('#checkBox').change(function() {
    const $checkbox = $(this);
    if ($checkbox.is(':checked')) {
    const lyftFlag = $checkbox.data("lyftFlag");
    drawLift(lyftFlag);
    }
    });
  • 关于javascript - 获取传单层控制复选框的状态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46792222/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com