jQuery getJSON - 可能的语法错误

发布于 2024-10-27 15:45:50 字数 1423 浏览 1 评论 0原文

好吧,我已经设法将各种示例中的以下内容放在一起,但是我什至不相信 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

情泪▽动烟 2024-11-03 15:45:50

一方面,您需要使用 JavaScript 的点表示法来引用对象属性,而不是 PHP 的箭头运算符:

data.total

而不是

data->total

For one thing, you need to use JavaScript's dot notation to reference object properties, and not PHP's arrow operator:

data.total

instead of

data->total
开始看清了 2024-11-03 15:45:50

我认为你在第 1 行的引用有问题,你似乎正在闯入 php,也许你需要使用更像是的东西:

<input name="quantity" type="text" id="quantity" onkeyup="showHint(this.value, ' . $events[0]['price'] . ' )" size="6" />

但如果没有完整的代码很难说

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:

<input name="quantity" type="text" id="quantity" onkeyup="showHint(this.value, ' . $events[0]['price'] . ' )" size="6" />

but hard to say without full code

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文