jQuery getJSON - 可能的语法错误
好吧,我已经设法将各种示例中的以下内容放在一起,但是我什至不相信 javascript 函数被调用,下面是我的所有代码:
Javascript:
<script type="text/javascript" src="coding/jquery.js"></script>
<script type="text/javascript">
function showHint(quantity, price){
document.getElementById("txtcost").innerHTML="being called values" + quantity + price;
$.getJSON('http://website.com/ecommerce/coding/validation/calprice.php', {q: quantity, p: price}, function(data){
$('#txtcost').html('Total: ' + data.total);
$('#txtvat').html('VAT: ' + data.vat);
});
}
</script>
显示
<span id="txtcost" style="color:#060"></span>
<span id="txtvat" style="color:#060"></span>
PHP
<?php
echo "being called";
function data() {
$q = $_GET["q"];
$p =&$_GET["p"];
$total = $p * $q;
if($q >= 10)
{
$total = ($total / 10) * 9;
//echo '<strong>£ ' . $total . '</strong><span style="font-size:10px; font-weight:bold;"> Discount applied.</span>';
echo json_encode(array('total' => $total, 'vat' => '10.00'));
}
else{ echo '<strong>£ ' . $total . '<strong>'; }}
?>
希望有所帮助
注意:代码并不完美,我想让初始功能正常工作。
编辑:
到目前为止我已经设法建立:
该函数正在被调用 正确的值被传递到函数中
Well I've managed to put the following together from various examples, however I don't believe the javascript function is even being called, below is all my code:
Javascript:
<script type="text/javascript" src="coding/jquery.js"></script>
<script type="text/javascript">
function showHint(quantity, price){
document.getElementById("txtcost").innerHTML="being called values" + quantity + price;
$.getJSON('http://website.com/ecommerce/coding/validation/calprice.php', {q: quantity, p: price}, function(data){
$('#txtcost').html('Total: ' + data.total);
$('#txtvat').html('VAT: ' + data.vat);
});
}
</script>
Display
<span id="txtcost" style="color:#060"></span>
<span id="txtvat" style="color:#060"></span>
PHP
<?php
echo "being called";
function data() {
$q = $_GET["q"];
$p =&$_GET["p"];
$total = $p * $q;
if($q >= 10)
{
$total = ($total / 10) * 9;
//echo '<strong>£ ' . $total . '</strong><span style="font-size:10px; font-weight:bold;"> Discount applied.</span>';
echo json_encode(array('total' => $total, 'vat' => '10.00'));
}
else{ echo '<strong>£ ' . $total . '<strong>'; }}
?>
Hope that helps
Note: the code isn't perfect, I want to get the initial functionality working.
EDIT:
SO far I've managed to establish:
The function is being called
The correct values are being passed into the function
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
一方面,您需要使用 JavaScript 的点表示法来引用对象属性,而不是 PHP 的箭头运算符:
而不是
For one thing, you need to use JavaScript's dot notation to reference object properties, and not PHP's arrow operator:
instead of
我认为你在第 1 行的引用有问题,你似乎正在闯入 php,也许你需要使用更像是的东西:
但如果没有完整的代码很难说
i think a problem with your quotes on line 1 you seem to be breaking into php, maybe you need to use something more like:
but hard to say without full code