javascript - 复选框onclick函数在另一个函数中

时间:2021-12-23 19:42:10

I'm building a web site with javascript. I have a leaflet map and some markers on it. I have a list and checkboxes on a div. I want to filter markers with checkboxes. But my onclick function can not reach marker's data. like this:

我正在使用javascript构建一个网站。我有一张传单地图和一些标记。我在div上有一个列表和复选框。我想用复选框过滤标记。但我的onclick功能无法达到标记的数据。像这样:

function onclick() {
    //delete marker
    //i can not reach marker's data from here
}

function initializeMap() {
    //something
    function markers() {
        //my markers defined here
        function onclickIwant() {
            //something
        }
    }
}

I want to use onclickIwant function for checkbox onclick or reach marker's data from onclick function. How can I do?

我想使用onclickIwant函数来复选onclick或从onclick函数到达marker的数据。我能怎么做?

my html:

<!DOCTYPE html>
<html>
  <head>
    <meta name="viewport" content="initial-scale=1.0, user-scalable=no">
    <meta charset="utf-8">
    <title>title</title>
    <link rel="stylesheet" type="text/css" href="style.css"/>
    <link rel="stylesheet" href="leaflet.css"/>
  </head>
    <body>
        <div id="harita"></div>
        <script src="leaflet.js"></script>
        <script src="index.js"></script>
        <div id="liste">
            <input type="checkbox" id="id1" checked="true" onclick="somefunction();"><label>id1</label>
            <input type="checkbox" id="id2" checked="true" onclick="somefunction();"><label>id2</label>
            <input type="checkbox" id="id3" checked="true" onclick="somefunction();"><label>id3</label>
            <ol id="Listesi"></ol>
        </div>
    </body>
</html>

1 个解决方案

#1


1  

I think you could something like this:

我想你可以这样:

function onclick() {
    //delete marker
    //i can not reach marker's data from here
    window.onclickIwant();
}

function initializeMap() {
    //something
    function markers() {
        //my markers defined here
        window.onclickIwant = function() {
            //something
        }
    }
}

Registering onclickIwant function into window object, you can use it anywhere.

将onclickIwant函数注册到window对象中,您可以在任何地方使用它。

Good luck!

#1


1  

I think you could something like this:

我想你可以这样:

function onclick() {
    //delete marker
    //i can not reach marker's data from here
    window.onclickIwant();
}

function initializeMap() {
    //something
    function markers() {
        //my markers defined here
        window.onclickIwant = function() {
            //something
        }
    }
}

Registering onclickIwant function into window object, you can use it anywhere.

将onclickIwant函数注册到window对象中,您可以在任何地方使用它。

Good luck!