我正在尝试替换邮政编码中的空格.我已经replace()在控制台中对它进行了测试并且它可以正常工作,但是在使用chrome进行调试时它无法逐步完成.
还有另一种方法可以让它发挥作用吗?
$('#search_postcode').on('click', function (e) {
e.preventDefault();
var getPostcode = $('#txt_search').val();
var postcode = {};
if (getPostcode !== "") {
var str = getPostcode;
str.replace(/\s+/g,'');
if (str.length === 7) {
postcode.Outward = str.substr(0, 4);
postcode.Inward = str.substr(4, 3);
}
if (str.length === 6) {
postcode.Outward = str.substr(0, 3);
postcode.Inward = str.substr(3, 3);
}
console.log(postcode);
Run Code Online (Sandbox Code Playgroud)
它总是带着空间回来 str
replace 返回一个新字符串,它不会更改传递的字符串(字符串在JavaScript中是不可变的).
使用
str = str.replace(/\s+/g,'');
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
147 次 |
| 最近记录: |