[英]Get ID of selected button using JavaScript
Currently I have a list of options (Which can be selected through clicking the button next to the option.) 目前,我有一个选项列表(可以通过单击选项旁边的按钮来选择。)
The following is the JavaScript code when I click the button. 以下是单击按钮时的JavaScript代码。
$('.step3').click(function() {
var os_name = $('.step3').attr('id');
alert(os_name);
var os_update;
os_update ="nothing so far";
if (os_name == "os_c_5_64") {
os_update = "centos-5-x86_64";
}
if (os_name == "os_c_6_64") {
os_update = "centos-6-x86_64";
}
if (os_name == "os_c_65_64") {
os_update = "centos-6.5-x86_64";
}
alert(os_update);
$('#vmos').val(os_update).change();
});
What I'm trying to do is get the ID of the actual selected button - Not directly from the class. 我想做的是获取实际选择的按钮的ID-不是直接从类中获取。
<tr class="appliance">
<td class="distro"><img src="<?=$this->tpl_asset_path;?>images/centos.jpg"></td>
<td class="description">Centos 5 64bit</td>
<td class="author">Linux distribution</td>
<td class="text-right"> <li class="step3" id="os_c_5_64" style="list-style: none;"><a class="btn btn-primary create" data-toggle="tab" href="#step3">Select</a></li></td>
</tr>
<tr class="appliance">
<td class="distro"><img src="<?=$this->tpl_asset_path;?>images/centos.png"></td>
<td class="description">Centos 6 64bit</td>
<td class="author">Linux distribution</td>
<td class="text-right"> <li class="step3" id="os_c_6_64" style="list-style: none;"><a class="btn btn-primary create" data-toggle="tab" href="#step3">Select</a></li></td>
</tr>
<tr class="appliance">
<td class="distro"><img src="<?=$this->tpl_asset_path;?>images/centos.png"></td>
<td class="description">Centos 6.5 64bit</td>
<td class="author">Linux distribution</td>
<td class="text-right"> <li class="step3" id="os_c_65_64" style="list-style: none;"><a class="btn btn-primary create" data-toggle="tab" href="#step3">Select</a></li></td>
</tr>
What's the best way to find the ID of the button I just clicked. 查找我刚刚单击的按钮的ID的最佳方法是什么。
I'm currently using: var os_name = $('.step3').attr('id');
我当前正在使用: var os_name = $('.step3').attr('id');
Which simply isn't working. 哪个根本行不通。
Within a jQuery event handler the this
keyword will refer to the element which raised the event. 在jQuery事件处理程序中, this
关键字将引用引发事件的元素。
As such, you can use either this.id
or $(this).prop('id');
因此,您可以使用this.id
或$(this).prop('id');
. 。 Note however that the former is by far the better practice. 但是请注意,到目前为止,前者是更好的做法。
请改用this.id
或jQuery版本$(this).attr("id")
Within your jQuery event handler (or in handlers registered with addEventListener
) you can just use this
to access the clicked element and therefore the below to get its ID: 在您的jQuery事件处理程序中(或在addEventListener
注册的处理程序中),您可以使用this
来访问clicked元素,因此可以通过以下操作获取其ID:
var os_name = this.id;
It's completely portable and maximally efficient. 它是完全便携式的,效率最高。
Please do not be tempted to use $(this).attr('id')
. 请不要试图使用$(this).attr('id')
A console.trace
for that is shown below: 相应的console.trace
显示如下:
f.extend.propjquery.min.js:2
f.extend.attrjquery.min.js:2
e.extend.accessjquery.min.js:2
f.fn.extend.attr
This shows that just the .attr
call alone requires a further three function calls internally, and that's even before you account for the overhead of calling $(this)
. 这表明仅.attr
调用就需要在内部再进行三个函数调用,这甚至还没有考虑到调用$(this)
的开销。
Using $(this).attr('id')
replaces a trivial property look up with at least five function calls! 使用$(this).attr('id')
用至少五个函数调用来替换琐碎的属性查找!
Are you using jQuery as well? 您是否也在使用jQuery? Try: 尝试:
$(this).attr('id');
In Javascript you can generally get an Element by Id using: 在Javascript中,通常可以使用以下方法通过ID获得Element:
document.getElementById("myButton") 的document.getElementById( “myButton的”)
if you have assigned it an Id. 如果您已为其分配一个ID。 If you have you might also be able to modify your code a little and use something like this: 如果有的话,您也许也可以稍微修改一下代码,并使用如下代码:
<script>
function getValue()
{
var x=document.getElementById("myHeader");
alert(x.innerHTML);
}
</script>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.