我正在使用 jQuery 来计算价格。这是我的 JavaScript 的一小段:
// Prices: radio
var curLam = "gloss";
$("input[name=lamination]").click(function () {
var gloss = 5;
var matt = 6;
if ($(this).val() == 'gloss' && curLam != 'gloss') {
$('#prices span').text(parseInt($('#prices span').text()) + gloss - matt);
curLam = 'gloss';
console.log('1');
}
if ($(this).val() == 'matt' && curLam != 'matt') {
$('#prices span').text(parseInt($('#prices span').text()) - gloss + matt);
curLam = 'matt';
console.log('2');
}
$("#prices span").each(function () {
var $priceValue = $(this);
})
});
这会检查是否选择了哑光或光泽饰面,然后将价格插入到 prices
div
内的 span
中标签。
我需要知道如何将该价格值分配给一个变量,然后将该变量传递给我的购物车的 PHP。
最佳答案
一般来说,您不应将所选价格返回到服务器。 HTML 表单很容易被伪造。将用户的选择发送回服务器要安全得多,服务器也知道它应该花费多少。
基本上,Javascript 验证(本质上就是这样)很方便,但不应该被信任。您已经有一个用于选择饰面的输入。只需将其发送回服务器即可。这样,即使用户禁用了 Javascript,您的网站仍然可以运行(或者运行得更好)。
关于php - 如何将变量从 JavaScript 传递到 PHP?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/980770/