javascript - jQuery 选择器 $("input[with="+id+"]")
问题描述
这个 jQuery 选择器实际上是如何工作的?
$("input[with="+id+"]")
在这个函数内部使用:
function enableFields(id) {
$("input[with="+id+"]").each(function() {
$(this).attr('disabled', false);
});
}
说“选择所有input
具有with
属性的元素。在当前input
元素上,将with
属性值设置为当前元素的id
属性值”是否正确?
本质上,如果输入元素看起来像<input id="idValue" with="withValue">
,则暂时将with
属性设置为id
从中查找的值<input id="idValue" with="idValue">
并使用它来选择要调用的元素each()
?
解决方案
仅在针对多个输入进行更新时才使用每个。使用 id 与类名让我认为你的意图是一次进行单个输入更新。不再使用 disabled false ,因为大多数现代浏览器将 disabled 的存在解释为“已禁用”。如果要更新多个输入,请使用 className 选择器而不是 id。
保持独特身份的精神:
function enableFields(id) {
$("#" + id).removeAttr('disabled');
}
使用类名更新多个输入:
function enableFields(className) {
$("." + className).each(function() {
$(this).removeAttr('disabled');
});
}
推荐阅读
- vb.net - 有没有办法将已经创建的按钮放入数组中?
- microservices - 启用 Hystrix 后 Autowire 不起作用
- python - Flask / jinja 如何计算平均值
- python - 无法运行旨在显示叠加蛋白质的python代码
- node.js - 我安装了最新的 nodejs 版本,但我也无法更新 npm (windows)
- svn - TortoiseSVN,在更新后(或提交前)更新文件中的修订
- python - 如何加载 Django 模板异步
- arrays - 将数组结构从第一个数组转换为第二个
- python - 如何根据python中的索引将两行变为单行
- python - 未找到符号:引用自:预期在:平面命名空间 | macOS 大苏尔