如何使用 Jquery 将所有输入值更改为大写?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15163570/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I change all input values to uppercase using Jquery?
提问by Gustavo Reyes
I want to change all my form values to uppercase before the form is submitted.
我想在提交表单之前将所有表单值更改为大写。
So far I have this but it's not working.
到目前为止,我有这个,但它不起作用。
$('#id-submit').click(function () {
var allInputs = $(":input");
$(allInputs).value.toUpperCase();
alert(allInputs);
});
回答by Selvakumar Arumugam
Try like below,
尝试如下,
$('input[type=text]').val (function () {
return this.value.toUpperCase();
})
You should use input[type=text]
instead of :input
or input
as I believe your intention are to operate on textbox only.
您应该使用input[type=text]
代替:input
或input
因为我相信您的意图是仅对文本框进行操作。
回答by Amrendra
use css :
使用 css :
input.upper { text-transform: uppercase; }
probably best to use the style, and convert serverside. There's also a jQuery plugin to force uppercase: http://plugins.jquery.com/plugin-tags/uppercase
可能最好使用样式,并转换服务器端。还有一个强制大写的 jQuery 插件:http: //plugins.jquery.com/plugin-tags/uppercase
回答by adeneo
回答by Adil
回答by Marco Vuillermoz
Use css text-transform to display text in all input type text. In Jquery you can then transform the value to uppercase on blur event.
使用 css text-transform 在所有输入类型文本中显示文本。在 Jquery 中,您可以在模糊事件上将值转换为大写。
Css:
css:
input[type=text] {
text-transform: uppercase;
}
Jquery:
查询:
$(document).on('blur', "input[type=text]", function () {
$(this).val(function (_, val) {
return val.toUpperCase();
});
});