javascript - 如何使用 jquery 从超链接 onclick 中获取 id?
问题描述
我有包含 `class="candidate_type" 的超链接。我想单击一个按钮并显示带有候选类型 ID 的锚的 ID。
<script>
$(document).ready(function(){
$("#continue").click(function(){
candidate_type = $(".candidate_type").attr('id');
alert(candidate_type);
});
});
</script>
<a class="candidate_type" id="Employer">I`m Employer</a>
<a class="candidate_type" id="Consultant">I`m Consultant</a>
<a href="javascript:void(0)" id="continue" class="btn-new">Continue</a>
谢谢你
解决方案
You have more than one candidate_type
elements, to get id of all elements you can iterate over it using .each
as shown below
$(document).ready(function(){
$("#continue").click(function(){
$(".candidate_type").each(function(){
var candidate_type = $(this).attr('id');
alert(candidate_type);
});
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<a class="candidate_type" id="Employer">I`m Employer</a>
<a class="candidate_type" id="Consultant">I`m Consultant</a>
<a href="javascript:void(0)" id="continue" class="btn-new">Continue</a>
推荐阅读
- angular - Angular 使用 ng2-file 上传器将多个文件转换为 base64 字符串
- android - 如何以编程方式设置 ConstraintLayout 约束
- vue.js - 在更大的项目中创建单个 Vue 组件
- javascript - Javascript onfocusout 事件不起作用
- android - Chrome Mobile:URL 栏导致页脚出现问题
- azure - Login-AzureRmAccount 和 Connect-AzureRmAccount 有什么区别?
- algorithm - 最长回文子序列(dp 解)
- c++ - 如何使用预设 url 和 Aws::FStream 将文件上传到 AWS S3?
- python-3.x - 如何在 Python3 中获取档案的文件名?
- python - 如何修复我的石头、纸、剪刀游戏的分数计数不显示?