How to change lowercase chars to uppercase using the 'keyup' event?

后端 未结 12 1830
失恋的感觉
失恋的感觉 2021-01-31 01:58

My goal is to use the jQuery event .keyup() to convert inputted lowercase chars to uppercase.

How can I achieve this?

12条回答
  •  野趣味
    野趣味 (楼主)
    2021-01-31 02:33

    jQuery:

    var inputs = $('#foo');
    
    inputs.each(function(){
              this.style.textTransform = 'uppercase';
           })
           .keyup(function(){
              this.value = this.value.toUpperCase();
           });
    

    1. Set the input's style to capitals (so user doesn't see the change)
    2. Automatically adjust the value (so the user doesn't have to hold shift or use caps lock)

提交回复
热议问题