[英]Javascript to remove spaces from .value
//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());
}
});
}
})
如何使用 JQuery 從 this.value 中刪除空間? 我嘗試放置if (jQuery.trim(this.value) == "")
,但它無法刪除內聯空格並且僅刪除前導和尾隨空格。
this.value = this.value.trim();
//you need to assign the trimmed value back to it.
請注意,在 IE < 9 版本中, trim可能不可用。 所以你可以使用:
this.value = $.trim(this.value);
$.trim()
函數從提供的字符串的開頭和結尾刪除所有換行符、空格(包括不間斷空格)和制表符。 如果這些空白字符出現在字符串的中間,它們將被保留。
如果要刪除前導和尾隨空格,請使用
//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());
}
});
}
});
如果你想內聯空格使用
//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());
}
});
}
});
這就是修剪在 javascript 中的工作方式。
var val = this.value.trim();
試試這個:
if(this.value.trim()==""){
//code here
}
.replace(//g,'') g 字符表示在整個字符串中重復搜索。 如果您想匹配所有空格,而不僅僅是文字空格字符,也可以使用 \\s:
.replace(/\\s/g,'')
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.