如何为POS系统下拉选项的多客户字段添加短横线分隔?
解决方案
很简单,你只需要在拼接三个字段的时候,在每两个字段之间加上' - '(短横线前后加空格)就可以实现想要的格式啦!
首先看你原来的代码里,拼接选项文本的这一行:
option.text = cust_name + cust_email + cust_mobile;
把它改成下面两种方式的任意一种都可以:
方式1:模板字符串(ES6+推荐写法,更简洁)
option.text = `${cust_name} - ${cust_email} - ${cust_mobile}`;
方式2:传统字符串拼接
option.text = cust_name + ' - ' + cust_email + ' - ' + cust_mobile;
另外注意到你代码里有个小拼写错误:var cust_emai = counter.cust_email;这里变量名少了个字母l,应该改成var cust_email = counter.cust_email;,不然会导致cust_email变量未定义的错误哦!
修改后的完整代码大概是这样:
for (var i = 0; i < jsonData.categories.length; i++) { var counter = jsonData.categories[i]; var cust_id = counter.cust_id; var cust_name = counter.cust_name; var cust_email = counter.cust_email; // 修正了拼写错误 var cust_mobile = counter.cust_mobile; option = document.createElement( 'option' ); option.value = cust_id; option.text = `${cust_name} - ${cust_email} - ${cust_mobile}`; // 用模板字符串实现分隔 select.add( option ); }
这样生成的选项文本就会是「Owais - owais@gmail.com - 03214834289」的格式啦。
内容的提问来源于stack exchange,提问作者Anderson
相关产品推荐
相关产品推荐

