我动态创建了输入,我需要从遗留代码运行函数。 这就是为什么我需要一些东西来允许我为每个动态创建的字段(id)运行单个函数。
foreach ($("input[id^='dynamicInput_']" as singleInput) {
do_something(singleInput);
}
有很多与动态创建字段相关的问题,但没有一个提供类似的解决方案。 我会欣赏可能的想法。 谢谢。
根据 KostasX 的回答,我做了这个:
$("input[id^='dynamicInput_']").each(function(){
doSomething(this);
});
它确实达到了目的。谢谢。
最佳答案
由于您使用的是 jQuery
,因此您可以在元素列表上使用 .each() 方法并将该函数应用于每个元素:
$("input[id^='dynamicInput_']").each(function( index, singleInput ){
doSomething( singleInput );
// Or wrap the singleInput in a jQuery object to execute jQuery methods:
doSomething( $(singleInput ) );
});
关于javascript - 动态创建字段/Id 中的 foreach 类似解决方案,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60366244/