简体   繁体   English

jQuery:input.val()中单词之间的$ .trim()空格

[英]jQuery: $.trim() spaces between words in input.val()

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). 意思是当用户专注于一个名为#destinput修剪它上面的所有空格字符(之前使用焦点输出来增加视觉舒适度)。

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? 我在OSX(Snow Leopard)10.6.8下使用Firefox和Safari,在10.8.2(Lion)下使用Safari,我得到了相同的结果...... OSX问题? -- Everything is ok, check my last edit! - 一切都好,查看我的上次编辑!


Final Edit and Solution thanks to Phil Klein 最终编辑和解决方案归功于Phil Klein

My problem was using incorrectly jQuery's trim() function... According to the trim() documentation it 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. $ .trim()函数从提供的字符串的开头和结尾删除所有换行符,空格(包括不间断空格)和制表符。 If these whitespace characters occur in the middle of the string, they are preserved. 如果这些空格字符出现在字符串的中间,则会保留它们。

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和所有帮助过我的人;)

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/

Try these : $.trim($("#dest").val()); 试试这些: $.trim($("#dest").val());
correct me if i am wrong !! 如果我错了,请纠正我!

try $("#dest").val().trim(); 尝试$("#dest").val().trim(); ,it worked for me. ,它对我有用。

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,}/, ' ')); 
 });
});

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));
});

如果您的代码在页面未完全就绪之前正在执行,则很可能jquery找不到#dest ,并且不会执行用于侦听事件的代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM