我有一个简单的问题。
我有以下代码:
$('.test').click(function (e)
{
e.preventDefault();
var arrayProducts = new Array();
$('.tableProducts').each(function(i)
{
if(typeof arrayProducts[i] == "undefined")
arrayProducts[i] = new Array();
$(this).find(':input').each(function(k)
{
if(k == 1)
{
var test = $(this).val();
arrayProducts[i].push(test+":0:0.0000");
}
else{
arrayProducts[i].push($(this).val());
}
});
});
var string = arrayProducts.join("|");
var productstring = string.replace(/,/g, ":");
$('#optionstring').append(productstring);
})
结果是例如:Opel:20:1:1:1:Suzuki:500:1:1:1
如您所见,该字符串是2个产品(Opel:20:1:1:1 and Suzuki:500:1:1:1
)
是否有可能在每次输入后创建,''不会变成':'像这样:
Opel:20:1:1:1,Suzuki:500:1:1:1
HTML:
<div class="container">
<div class="optiondiv">
<table class="tableOptions">
<tbody>
<tr>
<td><b>Option</b>:</td>
<td><b>Title</b>:</td>
<td><b>Input Type</b>:</td>
<td><b>Required</b>:</td>
<td><b>Position</b>: </td>
</tr>
<tr>
<td>
<button class="addproduct">Add</button>
<button class="deleteProduct">Delete</button>
</td>
<td>
<input type="text" class="title">
</td>
<td>
<select class="input">
<option value="select">Drop-down</option>
<option value="radio">Radio Buttons</option>
<option value="checkbox">Checkbox</option>
<option value="multi">Multiple Select</option>
</select>
</td>
<td>
<select class="required">
<option value="1">Yes</option>
<option value="0">No</option>
</select>
</td>
<td>
<input type="text" class="position">
</td>
</tr>
</tbody>
</table>
<table class="hiddenOption">
<tbody>
<tr>
<td><b>Option</b>:</td>
<td><b>Title</b>:</td>
<td><b>Input Type</b>:</td>
<td><b>Required</b>:</td>
<td><b>Position</b>: </td>
</tr>
<tr>
<td>
<button class="addproduct">Add</button>
<button class="deleteProduct">Delete</button>
</td>
<td>
<input type="text" class="title">
</td>
<td>
<select class="input">
<option value="select">Drop-down</option>
<option value="radio">Radio Buttons</option>
<option value="checkbox">Checkbox</option>
<option value="multi">Multiple Select</option>
</select>
</td>
<td>
<select class="required">
<option value="1">Yes</option>
<option value="0">No</option>
</select>
</td>
<td>
<input type="text" class="position">
</td>
</tr>
</tbody>
</table>
</div>
<div class="productdiv">
<table class="tableProducts">
<tbody>
<tr>
<td><b>SKU</b>:</td>
<td><b>Default Quantity</b>:</td>
<td><b>Position</b>:</td>
<td><b>Default</b>:</td>
<td><b>User defined</b>:</td>
</tr>
<tr>
<td>
<input type="text" class="sku">
</td>
<td>
<input type="text" class="quantity">
</td>
<td>
<input type="text" class="position">
</td>
<td>
<select class="isDefault">
<option value="1">Yes</option>
<option value="0">No</option>
</select>
</td>
<td>
<select class="userdefined">
<option value="1">Yes</option>
<option value="0">No</option>
</select>
</td>
</tr>
</tbody>
</table>
<table class="hiddenTemplate">
<tbody>
<tr>
<td><b>SKU</b>:</td>
<td><b>Default Quantity</b>:</td>
<td><b>Position</b>:</td>
<td><b>Default</b>:</td>
<td><b>User defined</b>:</td>
</tr>
<tr>
<td>
<input type="text" class="sku">
</td>
<td>
<input type="text" class="quantity">
</td>
<td>
<input type="text" class="position">
</td>
<td>
<select class="isDefault">
<option value="1">Yes</option>
<option value="0">No</option>
</select>
</td>
<td>
<select class="userdefined">
<option value="1">Yes</option>
<option value="0">No</option>
</select>
</td>
</tr>
</tbody>
</table>
</div>
<hr class="hr">
<button class="addOption">Create option</button>
<button class="deleteOption">Delete option</button>
<hr>
<button class="generateString">Genereer string</button>
<button class="test">Genereer products</button>
<hr>
<label>
Optionstring:<p id="optionstring"></p>
</label>
答案 0 :(得分:2)
您可以使用RegExp
将字符串拆分到正确的位置。在您的特定情况下 - 在字母前面的每个冒号处分开。
替换:var productstring = string.replace(/,/g, ":")
with:var productstring = string.split(/:(?=\D)/g).join(', ')
。
var str = 'Opel:20:1:1:1:Suzuki:500:1:1:1:Mazda:6:1:1:1';
console.log(str.split(/:(?=\D)/g).join(', '));
&#13;
答案 1 :(得分:1)
假设你有一个包含数组的数组,你需要用冒号连接内部数组,你可以映射内连接的数组并用逗号连接它。
var cars = [['Opel', 20, 1, 1, 1], ['Suzuki', 500, 1, 1, 1]],
string = cars.map(a => a.join(':')).join();
console.log(string);
&#13;
答案 2 :(得分:0)
我假设查看你的代码,你有一个类似如下的数组。
var arrayProducts = [['Opel','20','1','1','1'],['Suzuki','500','1','1','1']];
var productstring = '';
for(subarr of arrayProducts) {
productstring += (productstring !== '' ? ', ' : '') + subarr.join(':')
}
console.log(productstring ); //Opel:20:1:1:1, Suzuki:500:1:1:1
&#13;