jQuery each()方法失效:输入框失焦转大写仅单个生效
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

