jquery - 选择 .each() 函数在其上运行的元素
问题描述
我有这段代码,但elem
没有定义。也尝试过this
,$(this)
但得到了文件。
我应该提到 $('.listop') 是一个 jquery 对象数组(这就是我使用每个对象的原因)。在每个 listop 元素之前,它们是一个 radioOption 元素,我需要对它们每个元素,当一个人单击“listop”时,将检查“radioOption”。
let inputs = $('.listop');
$(document).ready(function() {
inputs.each(function(int, elem) {
elem.click(function() {
elem.closest('.radioOption').prop('checked', true);
});
})
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<tr>
<td>
<input type="checkbox" class="radioOption" name="distribution[only]" value="6" data-id="lonly"></td>
<td> לנמען יחיד</td>
<td>
<div id="lonly" class="listop" style="display: inline;">
<input type="text" name="mobile">
解决方案
我假设你的意思是这个
- 查找输入的 tr 父级
- 在该行中找到具有类 .radioOption 的复选框并检查它
注意,事件处理程序将对所有 .listop 输入起作用,并且仅选中同一行中的复选框
$(function() {
$(".listop input").on("click",function() {
$(this).closest("tr").find('.radioOption').prop('checked', true);
})
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<table>
<tr>
<td>
<input type="checkbox" class="radioOption" name="distribution[only]" value="6" data-id="lonly"></td>
<td> לנמען יחיד</td>
<td>
<div id="lonly" class="listop" style="display: inline;">
Mobile: <input type="text" name="mobile">
</div>
</td>
</tr>
<tr>
<td>
<input type="checkbox" class="radioOption" name="distribution[only]" value="6" data-id="lonly"></td>
<td> לנמען יחיד</td>
<td>
<div id="lonly" class="listop" style="display: inline;">
Mobile: <input type="text" name="mobile">
</div>
</td>
</tr>
<tr>
<td>
<input type="checkbox" class="radioOption" name="distribution[only]" value="6" data-id="lonly"></td>
<td> לנמען יחיד</td>
<td>
<div id="lonly" class="listop" style="display: inline;">
Mobile: <input type="text" name="mobile">
</div>
</td>
</tr>
</table>
推荐阅读
- python - 有没有办法在 python 代码转换为 .pyc 文件时检查 python 解释器版本?
- angular - 在显示所有 *ngFor 列表变量条目后执行操作
- kubernetes-ingress - Kubernetes Ingress 中的正则表达式路径未在 Nginx Ingress Controller 中创建为正则表达式位置
- visual-studio-code - vscode 用 eslint 格式化修改的行
- c# - 如何检查用户是否在 discord.NET 中具有角色?
- json - 当我使用 useEffect 从 API 获取对象时,无法将 json obj 存储在状态中
- html - 你如何设计一个有角度的材料表?
- javascript - 在 Angular 中将 [object HTMLElement] 显示为 HTML
- javascript - 将 JSON 添加到 Google Apps 脚本中的属性
- html - 看不到 tinymce 功能