jQuery onClick 捕获元素的id
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4384829/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
jQuery onClick capture the id of the element
提问by Harsha M V
i have many input fields like this
我有很多这样的输入字段
<input type="radio" name="name" id="name" onchange="enableTxt()" />
when i click this radio button i wanna capture the id of the radio input. i am using the following code
当我单击此单选按钮时,我想捕获无线电输入的 ID。我正在使用以下代码
function enableTxt() {
var id = $(this).attr("id");
alert(id);
}
Am getting this error
我收到这个错误
a.attributes is undefined
回答by Calvin
HTML:
HTML:
<input type="radio" name="name" id="name" onchange="enableTxt(this)" />
JS:
JS:
function enableTxt(elem) {
var id = $(elem).attr("id");
alert(id);
}
回答by Harry Bosh
The html
html
<button class="destroy" id="123">
The jQuery
jQuery
$('button.destroy').on('click', function(e){
e.preventDefault();
console.log(this.id);
回答by mikel
You can pass just the ID from the HTML
您可以只传递 HTML 中的 ID
<input type="radio" name="name" id="name" onchange="enableTxt($(this).attr('id'))" />
function enableTxt(id)
{
alert(id);
}
回答by kobe
pass this to the function
将此传递给函数
enableTxt(this)
function enableTxt(item) {
var id = $(item).attr("id");
alert(id);
}
回答by Rahul Chordiya
Try this
尝试这个
alert($("input:radio").attr('id'));
回答by Felipez
Another option is with JQ
另一种选择是使用 JQ
$("#name").on('onchange',enableText);
With this your object will be the current this
有了这个,你的对象将是当前的这个