目前我有一个选项列表(可以通过单击选项旁边的按钮来选择。)
下面是点击按钮时的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();
});
我想做的是获取实际选定按钮的 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>
查找我刚刚单击的按钮的 ID 的最佳方法是什么。
我目前正在使用:var os_name = $('.step3').attr('id');
这根本行不通。
最佳答案
使用 this.id
或 jQuery 版本 $(this).attr("id")
关于javascript - 使用 JavaScript 获取所选按钮的 ID,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29518762/