Javascript,获取单击按钮的名称 [英] Javascript, get the name of a clicked button
本文介绍了Javascript,获取单击按钮的名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
尝试获取已单击的按钮的ID或名称。但是当我尝试访问按钮的id或名称时,我只在弹出窗口中收到未定义的消息。有人能指出我正确的方向让这个工作吗?
Trying to get the id or name of the button that has been clicked. But I only get an undefined message in the popup when I try to access the id or name of the button. Could someone point me in the right direction to get this working?
<!DOCTYPE html PUBLIC "-//W2C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Testing : Buttons</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
</head>
<div id="buttons" style="width:100%;height:100%">
<p id="custom_buttons" >
</p>
</div>
<div id="footer">
</div>
<script type="text/javascript">
$(function() {
var datasetPresets = ["Python", "Java", "C++"];
var datasetPresetsContainer = $("#custom_buttons");
$.each(datasetPresets, function(index, value) {
datasetPresetsContainer.append("<button class=std_buttons value=" + value + ">" + value + "</button>");
});
$("button.std_buttons").click(function () {
var button = $(this);
alert(button.id);
});
});
</script>
</body>
</html>
</html
推荐答案
基本上,试试
alert(this.id);
alert(this.id);
而不是
alert(button.id);
alert(button.id);
但是这个问题显示了最小的研究工作,因为它在这里已被多次回答,并且简单的搜索就足够了:
But this question shows a minimum research effort, because it has been answered multiple times here, and a simple search would suffice:
how to get the id when a certain button is clicked using jquery
这篇关于Javascript,获取单击按钮的名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文