admin管理员组文章数量:1425006
//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,'')
本文标签: jqueryJavascript to remove spaces from valueStack Overflow
版权声明:本文标题:jquery - Javascript to remove spaces from .value - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745379565a2656096.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论