以下'发现'如果值包含字母字符,则选择器的值为NaN
:
form.find('input[name="product_name"]').val(); //results in NaN if alphabets
代码:
<script type="text/javascript">
$("document").ready(function(){
$('.add_to_cart').click(function() {
var form = $(this).closest('form');
var product_id = form.find('input[name="product_id"]').val();
var product_price = form.find('input[name="product_price"]').val();
var product_name = form.find('input[name="product_name"]').val(); //Alphanumeric value
var product_qty = form.find('input[name="product_qty"]').val();
var data = {
"action": "AddItem",
"product_id": +product_id,
"product_price": +product_price,
"product_name": +product_name,
"product_qty": +product_qty,
};
$.ajax({
type: 'POST',
dataType: "html",
url: "cart_array.php",
data: data,
success: function(data) {
$("#cart_total").html(data);
}
});
return false;
});
});
</script>
答案 0 :(得分:0)
问题在于变量名前面的+
:
var data = {
"action": "AddItem",
"product_id": product_id,
"product_price": product_price,
"product_name": product_name,
"product_qty": product_qty
};