javascript - 如何根据点击将新值存储在具有现有值的变量中?

标签 javascript jquery leaflet

我正在使用传单制作 map ,单击时,存储在 geojson 属性中的值将填充侧栏中的文本区域。我将使用这些值来构造一个将访问 API 的 URL。我的意图是用户可以单击 class =“selected”的任意三个框,并根据他或她的选择获取 map 上的信息。但我无法通过单击新变量来清除存储在 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>

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

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

最佳答案

你总是得到第一个 .selected 的值元素。使用 this 访问单击的元素点击里面的关键字event handler.

var groupSelected= $(this).val();

关于javascript - 如何根据点击将新值存储在具有现有值的变量中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38928953/

相关文章:

dictionary - Leaflet 和 Mapbox 是什么,它们有什么区别?

javascript - 如何让 DOM/事件监听器知道新附加的节点?

javascript - 如何使用 turn.js 进行缩放

javascript - 防止传单无限向左或向右平移

javascript - getImageData() 返回全 0

javascript - 通过 jQuery 添加事件到新创建的 DOM 元素

javascript - 传单 map 显示为灰色

用于数字检查和最大长度为 3 且不以 0 开头的 Javascript 正则表达式

javascript - 如何在函数生成的两个文件之间共享变量

php - 使用 PHP 通过 Skype 发送短信