JQUERY - 修剪功能不起作用

use*_*039 2 jquery input trim

修剪功能无法正常工作

<input class="input"></input>
<div class="button">CLICK</div>



$(".button").click(function() {

    var name = $( ".input" ).val(); 

    name = $.trim(name);

    console.log("TRIM " + name);    
});
Run Code Online (Sandbox Code Playgroud)

http://jsfiddle.net/5sufd9jj/

Jos*_*ier 5

修剪从字符串的开头和结尾删除空格.

如果要删除连续的空格'string string',请使用以下内容:

$.trim(name.replace(/\s+/g, ' '));
Run Code Online (Sandbox Code Playgroud)

更新的示例

$(".button").on('click', function() {
    var name = $.trim($('input').val().replace(/\s+/g, ' '));
    console.log("TRIM " + name);
});
Run Code Online (Sandbox Code Playgroud)
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input class="input"></input>
<div class="button">CLICK</div>
Run Code Online (Sandbox Code Playgroud)