如何在具有基于点击的现有值的变量中存储新值?

时间:2016-08-13 03:39:29

标签: javascript jquery leaflet

我正在使用传单制作地图,在点击时,存储在geojson属性中的值会在侧边栏中填充textareas。我将使用这些值来构建一个能够访问API的URL。我的意图是用户可以点击任何三个框,其中class =" selected"并根据他或她的选择在地图上获取信息。但我无法清除存储在groupSelected中的上一个变量,点击了新的变量。



//This grabs the geojson values and stores into the textarea tags in the sidebar

function checkInfo(e) {
	var properties = e.target.feature.properties;
	$("#nabe-name").html(properties.Name)
	$("#top-group").html(properties.largest)
	$("#second-group").html(properties.second)
	$("#third-group").html(properties.third)
};
//This is where I am stuck:
    $(".selected").on("click" ,function(e) {
	nabe = $("#nabe-name").val();
	var groupSelected= $(".selected").val();
}

<textarea id="nabe-name">Neighborhood</textarea>
<textarea  class ="selected" id="top-group">Top</textarea>
<textarea class ="selected" id="second-group">Second</textarea>   
<textarea class ="selected" id="third-group">Third</textarea>
&#13;
&#13;
&#13;

每次点击进入不同的textarea时,我都需要更改groupSelected变量。我必须考虑如何关闭我的函数,因为我需要AJAX请求可用的两个变量。

请帮忙。如果不是很明显,我是JS的新手。 THX

2 个答案:

答案 0 :(得分:1)

您始终获得第一个.selected元素的值。使用点击this

内的event handler.关键字访问点击的元素
var groupSelected= $(this).val();

答案 1 :(得分:0)

我认为groupSelected应该是一个全局变量来保存以前的值,并且会在下一次点击时更改。我的意思是代替在点击中创建var groupSelected它应该在点击监听器