print_r($getPrice)
结果是:$getPrice包含的元素不知道,可以是1个或2个或如下(3个元素)
Array
(
[0] => Array
(
[price_qty] => 2
[price] => 100.0000
)
[1] => Array
(
[price_qty] => 5
[price] => 90.0000
)
[2] => Array
(
[price_qty] => 8
[price] => 80.0000
)
)
jquery代码:get all the input value and make an addition
$(document).ready(function() { //wrap in a document.ready event handler
$('input').on('keyup', function() {
var result = 0;
$('.liste_couleur_qty li input').each(function() {
result += parseInt(this.value, 10);
});
$('div#qtyvalue').text(result);
});
});
现在,我想将结果与 $getPrice
数组中的 [price_qty]
进行比较。如果结果小于 0,则使用结果乘以 [price](来自 $getPrice 数组)值。
例如:
if result(from the jquery code) < 2, then price= result*110. then $('div#qtyvalue').text(result); change to $('div#qtyvalue').text("the qty is "+result+" the price is "+price);
if between 2--5(contain 5), the price is 100. if between 5--8(not contain 8), the price is 90
怎么做呢?谢谢。
有没有办法将 php 数组放入 javascript 数组中然后进行比较?
最佳答案
使用json_encode
PHP 函数将数组 $getPrice
转换为 JSON 字符串。然后将其分配给 JavaScript 变量。
更新:
<?PHP
$getPrice = array(
array("price_qty" => 2,"price" => 100.0000),
array("price_qty" => 4,"price" => 300.0000),
array("price_qty" => 6,"price" => 500.0000)
);
$json = json_encode($getPrice);
?>
在 jQuery 中解析它
<script>
$(document).ready(function () {
var parsedData = <?PHP echo $json; ?>;
for(k in parsedData)
alert(parsedData[k].price_qty);
});
</script>
关于php - 如何使用 php 输出并在 jQuery 中进行比较?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13490567/