最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

jquery - Javascript to remove spaces from .value - Stack Overflow

programmeradmin0浏览0评论
//loop thought all the sku field
$('.optionSku').each(function() {

    //if found field are empty
    if (jQuery.trim(this.value) == "") {

        //loop thought all the name field to copy to the empty field
        $('.optionName').each(function() {

            if ($(this).closest("tr").find(".optionSku").val() == "") {

                empty++;
                $(this).closest("tr").find(".optionSku").val($(this).val());
            }

        });
    }
})

How to remove space from this.value using JQuery? I try to put if (jQuery.trim(this.value) == ""), but it cannot remove the inline space and removes the leading and trailing spaces only.

//loop thought all the sku field
$('.optionSku').each(function() {

    //if found field are empty
    if (jQuery.trim(this.value) == "") {

        //loop thought all the name field to copy to the empty field
        $('.optionName').each(function() {

            if ($(this).closest("tr").find(".optionSku").val() == "") {

                empty++;
                $(this).closest("tr").find(".optionSku").val($(this).val());
            }

        });
    }
})

How to remove space from this.value using JQuery? I try to put if (jQuery.trim(this.value) == ""), but it cannot remove the inline space and removes the leading and trailing spaces only.

Share Improve this question edited Nov 17, 2016 at 10:00 WLatif 1,38811 silver badges19 bronze badges asked Nov 17, 2016 at 7:34 Ch HongCh Hong 3781 gold badge6 silver badges20 bronze badges 2
  • you could use 1)$(this).closest("tr").find(".optionSku").val($(this).val().trim()); or $(this).closest("tr").find(".optionSku").val($.trim($(this).val())); – gaurav malik Commented Nov 17, 2016 at 7:39
  • Hi! do want remove all the spaces, even those who are between the words? – Idir Ouhab Meskine Commented Nov 17, 2016 at 7:42
Add a ment  | 

5 Answers 5

Reset to default 3
this.value =  this.value.trim(); 
//you need to assign the trimmed value back to it.

Note that trim may not be available in IE < 9 version. So you can use:

this.value = $.trim(this.value);

The $.trim() function removes all newlines, spaces (including non-breaking spaces), and tabs from the beginning and end of the supplied string. If these whitespace characters occur in the middle of the string, they are preserved.
if you want to remove leading and trailing spaces use

//loop thought all the sku field
 $('.optionSku').each(function() {
    //if found field are empty
    if ($.trim(this.value)=="") {
       //loop thought all the name field to copy to the empty field
       $('.optionName').each(function() {
          if ($(this).closest("tr").find(".optionSku").val() == "") {
             empty++;
             $(this).closest("tr").find(".optionSku").val($(this).val());
          }
       });
    }
 });

if you want to inline spaces use

//loop thought all the sku field
 $('.optionSku').each(function() {
    //if found field are empty
    if (this.value.trim()=="") {
       //loop thought all the name field to copy to the empty field
       $('.optionName').each(function() {
          if ($(this).closest("tr").find(".optionSku").val() == "") {
             empty++;
             $(this).closest("tr").find(".optionSku").val($(this).val());
          }
       });
    }
 });

This is how trim work in javascript.

var val = this.value.trim();

Try this:

if(this.value.trim()==""){
    //code here
}

.replace(/ /g,'') The g character means to repeat the search through the entire string. If you want to match all whitespace, and not just the literal space character, use \s as well:

.replace(/\s/g,'')

发布评论

评论列表(0)

  1. 暂无评论