在输入上剥去空白区域

我有一个不需要任何空格的字段。 我需要删除任何输入内容。 这就是我正在尝试的…到目前为止没有运气

$('#noSpacesField').click(function() { $(this).val().replace(/ /g,''); }); 

使用jQuery trim删除前导和尾随空格

 $.trim(" test case "); // 'test case' 

删除所有空格…

 " test ing ".replace(/\s+/g, ''); // 'testing' 

删除输入的空格…

 $(function(){ $('#noSpacesField').bind('input', function(){ $(this).val(function(_, v){ return v.replace(/\s+/g, ''); }); }); }); 

实例

 $('#noSpacesField').keyup(function() { $(this).val($(this).val().replace(/ +?/g, '')); }); 

这将在您键入时删除空格,并且还将删除制表符char。

如果你只想要数字,试试这个! :d

 $("#id").keyUp(function(){ if(isNaN($(this).val())) { $(this).val(0); } $(this).val($(this).val().replace(/ +?/g, '')); })