我有一个不需要任何空格的字段.我需要删除任何输入.这是我正在尝试…没有运气到目前为止
$('#noSpacesField').click(function() { $(this).val().replace(/ /g,''); });
解决方法
使用jQuery修剪来删除前导和尾随的空格
$.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,''); }); }); });
Live Example