开发者

Create comma separated value from multiple fields

开发者 https://www.devze.com 2023-03-19 11:35 出处:网络
I have between 1-10 form fields posted to a php script that collects the values from existing fields and use that data

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

精彩评论

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