Javascript jQuery:input.val() 中单词之间的 $.trim() 空格
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8570641/
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
jQuery: $.trim() spaces between words in input.val()
提问by TCB13
I've seen some similar questions to mine here, but they don't really answer me...
我在这里看到了一些与我类似的问题,但他们并没有真正回答我......
So I'm doing this: (Inside the document ready function)
所以我这样做:(在文档准备功能中)
$("#dest").focusin(function() {
$("#dest").val($.trim($("#dest").val()));
});
The ideia is when the user focus on a input
called #dest
trim all the space characters on it (previously added using focusout for visual comfort).
ideia 是当用户将注意力集中在一个input
称为#dest
修剪它的所有空格字符上时(以前使用 focusout 添加以提高视觉舒适度)。
Right now, nothing is happening. :(
现在,什么都没有发生。:(
Hope someone can help me a bit here.
希望有人能在这里帮助我。
Thanks!
谢谢!
Is this a computer related problem?I've tested all the code provided by commenters and none works. I'm using Firefox and Safari under OSX (Snow Leopard) 10.6.8 and also Safari under 10.8.2 (Lion) and I got the same results... OSX problem? -- Everything is ok, check my last edit!
这是电脑相关的问题吗?我已经测试了评论者提供的所有代码,但没有一个有效。我在 OSX (Snow Leopard) 10.6.8 下使用 Firefox 和 Safari,也在 10.8.2 (Lion) 下使用 Safari,我得到了相同的结果...... OSX 问题?--一切正常,检查我上次的编辑!
Final Edit and Solution thanks to Phil Klein
感谢 Phil Klein 的最终编辑和解决方案
My problem was using incorrectly jQuery's trim()
function... According to the trim()
documentationit does the following:
我的问题是错误地使用了 jQuery 的trim()
函数......根据trim()
文档,它执行以下操作:
The $.trim() function removes all newlines, spaces (including non-breaking spaces), and tabs from the beginning and end of the supplied string. If these whitespace characters occur in the middle of the string, they are preserved.
$.trim() 函数从提供的字符串的开头和结尾删除所有换行符、空格(包括不间断空格)和制表符。如果这些空白字符出现在字符串的中间,它们将被保留。
Yesterday I didn't read the last part where it says from the beginning and end of the supplied string
-- Sorry everyone. :(
昨天我没有读到最后一部分from the beginning and end of the supplied string
- 对不起大家。:(
Lucky and after the drawing above, @Phil Klein understood my mistake and helped me with a solution:
幸运的是,在上面的绘图之后,@Phil Klein 理解了我的错误并帮助我解决了问题:
$(function() {
$("#dest").on("focus", function() {
var dest = $(this);
dest.val(dest.val().split(" ").join(""));
});
});
You can read more about the solution and see an example here.
Thanks to @Phil Klein and also everyone who helped me on this one ;)
感谢@Phil Klein 以及所有帮助过我的人 ;)
回答by Phil Klein
The example below removes all spaces from the contents of the textbox on focus. This particular example requires jQuery 1.7+ since it uses the new .on()
API:
下面的示例从焦点文本框的内容中删除所有空格。这个特殊的例子需要 jQuery 1.7+,因为它使用了新的.on()
API:
$(function() {
$("#dest").on("focus", function() {
var dest = $(this);
dest.val(dest.val().split(" ").join(""));
});
});
See this example: http://jsfiddle.net/RnZ5Y/5/
看这个例子:http: //jsfiddle.net/RnZ5Y/5/
回答by Jigar Tank
Try these : $.trim($("#dest").val());
correct me if i am wrong !!
试试这些:$.trim($("#dest").val());
如果我错了,请纠正我!!
回答by Hyman Sun
try $("#dest").val().trim();
,it worked for me.
尝试$("#dest").val().trim();
,它对我有用。
回答by Shiva Brahma
I made my own function :)
我做了我自己的功能:)
look here please :)
请看这里:)
function KillSpaces(phrase) {
var totalPhrase = "";
for (i = 0; i < phrase.length; i++)
{
if (phrase[i] != " ")
{
totalPhrase += phrase[i];
}
}
return totalPhrase;
}
you can easily use it when ever you want to!
您可以随时轻松使用它!
$(document).ready(function(){
var test="for testing purposes only";
alert(killSpaces(test));
});
回答by Roman
If you're code above is executing before the page isn't fully ready then it's very likely that the #dest
isn't found by jquery and the code for listening to the event doesn't get executed.
如果上面的代码在页面没有完全准备好之前正在执行,那么很可能#dest
是 jquery 找不到并且侦听事件的代码没有被执行。
回答by Innovator One
This function is working fine for your scenario. As it is taking only one space between character and not allow more than 2 space
此功能适用于您的场景。因为它只占用一个字符之间的空格,并且不允许超过 2 个空格
$(function() {
$("#dest").on("focusout", function() {
var dest = $(this);
dest.val(jQuery.trim(dest.val()));
dest.val(dest.val().replace(/[ ]{2,}/, ' '));
});
});