jQuery remove spaces on blur not working

巧了我就是萌 提交于 2019-12-25 07:48:54

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!