You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

jQuery each()方法失效:输入框失焦转大写仅单个生效

Fixing Batch Blur Event Binding for Uppercase Conversion in jQuery

Hey there, let's get this sorted out right away! The root cause of your issue is a syntax error in your each() method code—you're missing the closing }); that wraps up the each() function. This error breaks the execution of your code, which is why only one input ends up working (or potentially none beyond the first, depending on how the browser handles the syntax mistake).

Corrected Code with each()

Here's the fixed version of your batch binding code, with the missing closure added:

$("#input1,#input2").each(function() {
  $(this).blur(function() {
    $(this).val($(this).val().toUpperCase());
  });
}); // This closing pair was missing in your original code

Even Simpler Approach (No each() Needed)

Actually, you don't even need the each() loop at all! jQuery automatically iterates over all elements in the matched collection when you bind an event directly to it. You can simplify your code to this, which works exactly the same:

$("#input1,#input2").blur(function() {
  $(this).val($(this).val().toUpperCase());
});

This will ensure that whenever either input loses focus (blur event triggers), its content is converted to uppercase immediately.

内容的提问来源于stack exchange,提问作者Zeeshan

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.29 07:35:42