javascript - 带有复选框和 anchor 标记的 jquery

标签 javascript html jquery-ui jquery

如果我在 anchor 标记旁边有一个复选框,我如何使用 jQuery 在单击该复选框时显示带有 anchor 标记 href 值的警告框。

例如,

<div><input type="checkbox"><a href="http://link0">link 0</a></div>
<div><input type="checkbox"><a href="http://link1">link 1</a></div>
<div><input type="checkbox"><a href="http://link2">link 2</a></div>
<div><input type="checkbox"><a href="http://link3">link 3</a></div>
<div><input type="checkbox"><a href="http://link4">link 4</a></div>
<div><input type="checkbox"><a href="http://link5">link 5</a></div>
<div><input type="checkbox"><a href="http://link6">link 6</a></div>
<div><input type="checkbox"><a href="http://link7">link 7</a></div>
<div><input type="checkbox"><a href="http://link8">link 8</a></div>
<div><input type="checkbox"><a href="http://link9">link 9</a></div>

如果我点击 oto 链接 7 旁边的复选框,它应该提醒我

http://link7

等等。

最佳答案

像这样:

$(':checkbox').click(function() { 
    alert($(this).nextAll('a:first').attr('href'));
});

编辑:解释:

  • :checkbox selector选中所有复选框。
  • $(this).nextAll('a:first')会找到第一个 <a> this 之后的元素,即使中间还有其他元素。
    相比之下,$(this).next('a') will only return the next element if it's an `;如果介于两者之间,则不会返回任何内容。

关于javascript - 带有复选框和 anchor 标记的 jquery,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4627169/

相关文章:

html - 如何管理多个 HTML 电子邮件模板而不失去理智?

javascript - 播放不同时长的多个视频?

JQuery 自动完成 : Preventing text replace when focus

jquery - 在运行 javascript 代码之前延迟

javascript - 更改 jQuery UI 模式对话框的堆栈顺序

来自变量的 Javascript 正则表达式测试

javascript - 从数组中删除项目,重新排序项目,然后放回去(reactjs)

JavaScript/jQuery 在两个输入字段都填写时专门更改标签的类

javascript - 我需要用引号引起来的匹配字符串

html - 列表项元素符号重叠