[英]Remove space on textbox but not between words
I want to remove spaces on the beginning of text on textbox on keyup event but will not remove spaces if it is between text. 我想在keyup事件中删除文本框上文本开头的空格,但是如果在文本之间,则不会删除空格。 how can i do that?
我怎样才能做到这一点?
<script language="javascript">
String.prototype.ltrim = function() {
return this.replace(/^\s+/,"");
}
$(function(){
$("#idOfTextBoxHere").keyup(function(){
$(this).val( $(this).val().ltrim());
});
});
</script>
Hi i don know your exact need ple correct me 嗨,我不知道您的确切需要,请纠正我
$('#your textbox id').keyup(function () {
var val = $(this).val();
val = val.replace(/^\s+/, '');
$(this).val(val);
});
Use the regular expression 使用正则表达式
/^\ //
to remove the space at the beginning of the line. 删除行首的空格。
use the trim()
function 使用
trim()
函数
var text = " hel lo ";
alert(text.trim());
Try: 尝试:
$("#idOfTextboxHere").focusout(function(){
$(this).val( $.trim($(this).val()) );
});
When the user clicks away, it will trim the beginning and end of the textbox. 当用户单击离开时,它将修剪文本框的开头和结尾。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.