开发者

Create comma separated value from multiple fields

I have between 1-10 form fields posted to a php script that collects the values from existing fields and use that data

Since I am trying to change some functions on the site to ajax I wonder how I can, with jquery, find all the fields with the name amount-*and put their values in a comma separated string and then post it to the server side script via ajax like this:

New code

$("#div").load("/serverscript.php", {ids:commaSeparatedValues}

Old code

while(isset开发者_运维问答($_POST['amount-'.$indexCount])){
  $changedCartAmount = $changedCartAmount . $_POST['amount-'.$indexCount] . ',';
  $indexCount++;
}
$changedCartAmount = substr($changedCartAmount,0,-1);
$arrayChangedCartAmount = explode(",",$changedCartAmount);


var commaSeparatedValues = [];
$("[name^='amount-']").each(function() {
  var val = this.value; // using the raw field value rather than $(this).val();
  if (val.length>0) commaSeparatedValues.push(val);
}
$("#div").load("/serverscript.php", {ids:commaSeparatedValues.join(",")}


Try this:

var commaSeperatedValues = "";
$("[name^='amount-']").each(function(){
    commaSeperatedValues += $(this).val() + ","; //or you can use this.value
});
commaSeperatedValues = commaSeperatedValues.replace(/,$/, "")

$("#div").load("/serverscript.php", {ids:commaSeparatedValues});
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜