问题
The replace function I'm using works as I've tested it in the console but it does not work at all on blur. How can I fix that?
<script type="text/javascript">
//Ensure spaces are not entered between tags
jQuery(document).ready(function(){
jQuery('#item_keywords').blur(function(){
$(this).val().replace(/^\s+$/,"");
});
});
</script>
回答1:
you have removed the spaces but u have not assigned them any where :)
jQuery(document).ready(function(){
jQuery('#item_keywords').unbind('blur').bind('blur',function(){
$(this).val($(this).val().replace(/^\s+$/,""));
});
});
回答2:
I'm using:
jQuery(document).ready(function(){
jQuery('#business_email').unbind('blur').bind('blur',function(){
var a = $(this).val().trim();
$(this).val(a + '.');
$(this).val(a);
});
});
回答3:
You can try this.
$(document).ready(function(){
$("#mytextbox").blur(function(){
$(this).val($(this).val().replace(/\s/g,''));
console.log($(this).val());
});
});
来源:https://stackoverflow.com/questions/5040757/jquery-remove-spaces-on-blur-not-working