//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.
- 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
5 Answers
Reset to default 3this.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,'')
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745379566a4625160.html
评论列表(0条)