I have a problem with my code. I am creating a form that has multiple textbox and what my process is. If the user checked the checkbox, there is a function for setting the product ID in the array. If the user unchecked, the product ID in the array must removed. I am using the checkbox onclick trigger effect.
Here's my sample code:
function setProduct(product_id) {
var arr_product = [];
//if existed, removed in the array
if($.inArray(product_id), arr_product) {
arr = jQuery.grep(arr_product, function(value) {
return value != product_id;
});
} else {
arr_product.push(product_id);
}
return arr_product;
}
function viewSetItems(product_id, product_name, image) {
var url_link = 'index.php?route=product/product/getSetProducts&product_id=' + product_id;
$.ajax({
url: url_link,
type: 'post',
dataType: 'json',
beforeSend: function() {
$("#display-set-item").fadeIn();
},
success: function(data) {
if(image == '') {
image = "<?php DIR_IMG_DATA ?>image/cache/data/no_image-200x200.jpg";
}
$("#p-img").prepend("<img src='"+image+"' height='150' width='150' class='img-responsive'/>");
$("#p-img").attr("rowspan",data.length + 1);
var prod_array = [];
data.forEach(function(i){
var p_array = i.product_id + "_" + i.price;
prod_array.push(p_array);
name = product_name.replace("||", " "); // value = 9:61
$("#p-name").text(name);
$h_table = '';
$nname = i.name;
$p_name = $nname.replace("||", " ");
$link = 'index.php?route=product/product&product_id=' + i.product_id;
$h_table+="<tr class='p-detail'>";
$h_table+="<td class='text-center'><input type='checkbox' name='p_price[]' id='pp-"+i.product_id+"' value="+i.price+" onClick='setProduct("+i.product_id+")' /></td>";
$h_table+="<td><a href='"+$link+"' class='modal-link'>" +$p_name+ "</a></td>";
$h_table+="<td class='text-right'>" +i.price+ "</td>";
$h_table+="</tr>";
$("#table-product-set").append($h_table);
});
prod_array.forEach(function(d) {
var x = d.split("_");
setProduct(x[0]);
});
alert(setProduct());
$('#table-product-set tr:last').after("<tr class='p-action'><td colspan='4'><span class='btn-blue pull-right' id='close-btn' style='cursor: pointer;' onclick='closeMe()'>CLOSE</span><input type='button' class='btn-blue pull-right' value='ADD TO REGISTRY' style=' margin-right: 5px' /></td></tr>");
},
error: function() {
$("#display-set-item").text('Error');
}
});
}
Sorry I have a little knowledge in jquery/javascript. I hope you can help me. Ok that's all guys, Thanks.