将数组值输出为URL中的单个逗号分隔行

时间:2019-03-14 22:00:47

标签: javascript arrays url checkbox

我已经尝试过,但是仍然得到错误的输出。我得到以下URL输出:

  

php?SKU%5B%5D = B117&SKU%5B%5D = 264B&SKU%5B%5D = KVS1

当我需要这个时:

  

.php?SKU = B117,264B,KVS1

它正确显示在警报框中,但未正确显示在URL中。任何帮助将是 非常感谢。

<form name=form1  onsubmit="chkcontrol()" method=get>
<div><input type=checkbox name=SKU[] value=B117>Add to Compare</div>
<div><input type=checkbox name=SKU[] value=264B>Add to Compare</div>
<div><input type=checkbox name=SKU[] value=KVS1>Add to Compare</div>
<div><input type=checkbox name=SKU[] value=1664>Add to Compare</div>
<div><input type=checkbox name=SKU[] value=PA03>Add to Compare</div>
<input type="submit" value="Compare" class="fsSubmitButton" />
</form>

 function chkcontrol() {
 var checkboxes = document.getElementsByName('SKU[]');
var mpn = "";
var total=0;
for (var i=0, n=checkboxes.length;i<n;i++) 
{
if (checkboxes[i].checked) 
{
    mpn += ","+checkboxes[i].value;
    total =total +1;
}
}
if (total < 2 || total > 4) {
alert("Please select 2 - 4 models to Compare") 
return false;
 } 
 if (mpn) mpn = mpn.substring(1);
 alert("SKU=" + mpn)
 alert("View your Compare Table below")
 }      `

0 个答案:

没有答案