JS:
<script type="text/css">
$(function() {
$('#upper').keyup(function() {
this.value = this.value.toUpperCase();
});
});
</script>
HTML
<div id="search">
<input type="radio" name="table" class="table" value="professor" tabindex="1" /> Professor
<input type="radio" name="table" class="table" value="department" tabindex="2" /> Department
<input type="radio" name="table" id="upper" class="table" value="course" tabindex="3" /> Course
<input type="text" name="search" class="keywords" value="Select an option..." onclick="this.value='';" onfocus="this.select()" onblur="this.value=!this.value?':this.value;" tabindex="4" />
<div id="content"> </div>
</div>
为什么这仍然不起作用?只是试图从JS中调用div“.keywords”。
答案 0 :(得分:225)
我认为最优雅的方式是没有任何javascript但使用css。您可以使用text-transform: uppercase
(这仅仅是为了这个想法):
<input id="yourid" style="text-transform: uppercase" type="text" />
修改强>
因此,在您的情况下,如果您希望关键字为大写更改:
keywords: $(".keywords").val(),
至$(".keywords").val().toUpperCase(),
答案 1 :(得分:46)
Javascript字符串对象具有toLocaleUpperCase()
功能,使转换本身变得容易。
$(function() {
$('input').keyup(function() {
this.value = this.value.toLocaleUpperCase();
});
});
不幸的是,这会完全重置文本框内容,因此用户的插入位置(如果不是“文本框的末尾”)将丢失。
但是,你可以hack this back in使用一些浏览器切换魔法:
// Thanks http://blog.vishalon.net/index.php/javascript-getting-and-setting-caret-position-in-textarea/
function getCaretPosition(ctrl) {
var CaretPos = 0; // IE Support
if (document.selection) {
ctrl.focus();
var Sel = document.selection.createRange();
Sel.moveStart('character', -ctrl.value.length);
CaretPos = Sel.text.length;
}
// Firefox support
else if (ctrl.selectionStart || ctrl.selectionStart == '0') {
CaretPos = ctrl.selectionStart;
}
return CaretPos;
}
function setCaretPosition(ctrl, pos) {
if (ctrl.setSelectionRange) {
ctrl.focus();
ctrl.setSelectionRange(pos,pos);
}
else if (ctrl.createTextRange) {
var range = ctrl.createTextRange();
range.collapse(true);
range.moveEnd('character', pos);
range.moveStart('character', pos);
range.select();
}
}
// The real work
$(function() {
$('input').keyup(function() {
// Remember original caret position
var caretPosition = getCaretPosition(this);
// Uppercase-ize contents
this.value = this.value.toLocaleUpperCase();
// Reset caret position
// (we ignore selection length, as typing deselects anyway)
setCaretPosition(this, caretPosition);
});
});
最终,伪造它可能是最容易的。在文本框上设置样式text-transform: uppercase
,使其以大写显示给用户,然后在Javascript中,只要用户的插入符号完全离开文本框,就会应用文本转换:
HTML:
<input type="text" name="keywords" class="uppercase" />
CSS:
input.uppercase { text-transform: uppercase; }
使用Javascript:
$(function() {
$('input').focusout(function() {
// Uppercase-ize contents
this.value = this.value.toLocaleUpperCase();
});
});
希望这有帮助。
答案 2 :(得分:6)
也可以这样做,但其他方式似乎更好,如果你只需要它一次就会派上用场。
onkeyup="this.value = this.value.toUpperCase();"
答案 3 :(得分:5)
尝试:
$('#search input.keywords').bind('change', function(){
//this.value.toUpperCase();
//EDIT: As Mike Samuel suggested, this will be more appropriate for the job
this.value = this.value.toLocaleUpperCase();
} );
答案 4 :(得分:4)
如果您将其用于 html输入, 你可以轻松地做到这一点,而无需使用javascript。 这将是标准的并且非常容易使用现代CSS标签:
<input type="text" style="text-transform: uppercase" >
或者您可以使用名为&#34; text-uppercase&#34;
的引导类<input type="text" class="text-uppercase" >
答案 5 :(得分:4)
使用value.toUpperCase的解决方案似乎存在这样的问题:在字段中键入文本会将光标位置重置为文本的末尾。使用文本转换的解决方案似乎存在提交给服务器的文本仍然可能是小写的问题。该解决方案避免了这些问题:
function handleInput(e) {
var ss = e.target.selectionStart;
var se = e.target.selectionEnd;
e.target.value = e.target.value.toUpperCase();
e.target.selectionStart = ss;
e.target.selectionEnd = se;
}
<input type="text" id="txtTest" oninput="handleInput(event)" />
答案 6 :(得分:3)
我找不到其中一个答案中引用的Bootstrap中的text-uppercase。无论如何,我创造了它;
.text-uppercase {
text-transform: uppercase;
}
以大写形式显示文本,但不会以这种方式转换基础数据。 所以在jquery我有;
$(".text-uppercase").keyup(function () {
this.value = this.value.toLocaleUpperCase();
});
这将在您使用text-uppercase类的任何地方更改基础数据。
答案 7 :(得分:2)
答案 8 :(得分:2)
我认为最简单的方法是使用Bootstrap的类".text-uppercase"
<input type="text" class="text-uppercase" />
答案 9 :(得分:1)
这个答案有一个问题:
style="text-transform: uppercase"
它还将占位符字转换为不方便的大写
placeholder="first name"
在渲染输入时,它将“名字”占位符写为大写
FIRST NAME
所以我写了更好的东西:
onkeypress="this.value = this.value + event.key.toUpperCase(); return false;"
效果很好!但如果您的javascript代码很复杂,它会产生一些副作用,
希望有人能让他/她想出一个更好的解决方案。
答案 10 :(得分:0)
Javascript有toUpperCase()
方法。 http://www.w3schools.com/jsref/jsref_toUpperCase.asp
因此,只要您认为最好将其放入代码中,就必须执行类似
的操作$(".keywords").val().toUpperCase()
答案 11 :(得分:0)
**JAVA SCRIPT**
<html>
<body>
<script>
function @ToCaps(obj)
{
obj.value=obj.value.toUpperCase();
}
</script>
<input type="text" onkeyup=@ToCaps(this)"/>
</body>
</html>
**ASP.NET**
Use a css style on the text box, write css like this:
.ToCaps {text-transform:uppercase; }
<asp:TextBox ID="TextBox1" runat="server" CssClass="ToCaps"></asp:Te writxtBox>
**OR**
simply write this code in textbox
<asp:TextBox ID="TextBox1" runat="server" style="text-transform:uppercase"></asp:TextBox>
**1.Note you don't get intelligence until you type up to style="**
答案 12 :(得分:0)
你可以试试这个 HTML
<input id="pan" onkeyup="inUpper()" />
的javaScript
function _( x ) {
return document.getElementById( x );
}
// convert text in upper case
function inUpper() {
_('pan').value = _('pan').value.toUpperCase();
}
答案 13 :(得分:0)
<input id="name" data-upper type="text"/>
<input id="middle" data-upper type="text"/>
<input id="sur" data-upper type="text"/>
具有属性的动态创建的元素上的文本 上部以及何时进行击键动作
$(document.body).on('keyup', '[data-upper]', function toUpper() {
this.value = this.value.toUpperCase();
});
答案 14 :(得分:0)
在此,我们在输入字段中使用 onkeyup 事件,该事件在用户释放键时触发。在这里,我们通过 toUpperCase()函数将值更改为大写。
请注意, text-transform =“大写” 只会更改样式的文本。但不是很有价值。因此,为了更改值,请使用将显示并更改值的此内联代码
<input id="test-input" type="" name="" onkeyup="this.value = this.value.toUpperCase();">
这是证明值已更改的代码段
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<form method="get" action="">
<input id="test-input" type="" name="" onkeyup="this.value = this.value.toUpperCase();">
<input type="button" name="" value="Submit" onclick="checking()">
</form>
<script type="text/javascript">
function checking(argument) {
// body...
var x = document.getElementById("test-input").value
alert(x);
}
</script>
</body>
</html>