在AJAX GET调用(PHP)之后JQuery没有替换内容

时间:2012-04-22 15:26:25

标签: php jquery ajax get

更改INPUT值后,内容将被微调器替换,并且"请等待..."消息,一旦从GET获得响应,我想用返回的新HTML替换微调器/消息,但返回的新HTML不会替换微调/消息。

下面是(动态PHP)HTML。

<script type="text/javascript">

    $(document).ready( function() {
        $( '#submit' ).click( function( e ) {
            e.preventDefault();

        });
    });

</script>

<script type="text/javascript">

    $(document).ready( function() {
        $( '.basket-item' ).children( 0 ).children( 1 ).change( function( e ) {
            e.preventDefault();

            var q = $( this ).val();
            var id = $( this ).parent().children( 0 ).val();

            var elem = $( this ); // var content = elem.parent().parent().parent().html();

            elem.parent().parent().parent().html( '<div class="basket-item item-border" style="width:896px;text-align:center;"><p class="para"><img src="<?php echo $this -> get( '__baseuri' ); ?>/media/images/spinner.gif" />Please wait...</p></div>' );

            var url = '<?php echo $this -> get( '__baseuri' ); ?>shop/adjust/' + id + '/?quantity=' + q;

            $.get( url, function( data ) {
                elem.parent().parent().parent().html( data );

            });




        });
    });

</script>

<h1>Shopping Basket</h1>

<div>
    <div class="basket-item" style="width:512px;text-align:left;"><p>Product / Item</p></div>
    <div class="basket-item"><p>Quantity</p></div>
    <div class="basket-item"><p>Price &pound;</p></div>
    <div class="basket-item"><p>Total &pound;</p></div>
</div>

<?php if( $this -> has( 'records' ) ): ?>
<form
    method="post"
    action="#"
    accept-charset="utf-8">

<?php foreach( $this -> get( 'records' ) as $record ): ?>
    <div>
        <div class="basket-item item-border" style="width:512px;text-align:left;">
            <p class="para">
                <img 
                    width="48" 
                    height="48" 
                    alt="<?php echo $record -> get( 'name' ); ?> (&pound;<?php echo $record -> get( 'price' ); ?>)" 
                    src="<?php echo $this -> get( '__baseuri' ); ?>media/images/products/generic/<?php echo $record -> get( 'image' ); ?>.jpg" />
            <a href="<?php echo $this -> get( '__baseuri' ); ?>products/<?php echo $record -> get( 'tainted' ); ?>/"><?php echo $record -> get( 'name' ); ?></a></p>
        </div>
        <div class="basket-item item-border">
            <p>
                <input type="hidden" name="product" value="<?php echo $record -> get( 'tainted' ); ?>" />
                <input type="text" name="quantity" size="3" maxlength="3" value="<?php echo $record -> get( 'quantity' ); ?>" />
            </p>
        </div>
        <div class="basket-item item-border"><p><?php echo $record -> get( 'price' ); ?></p></div>
        <div class="basket-item item-border"><p>&nbsp;</p></div>
    </div>
<?php endforeach; ?><div style="clear:both;"></div>
<div>
    <div class="basket-item item-border" style="width:768px;text-align:right;"><p>Grant Total &pound;&nbsp;</p></div>
    <div class="basket-item item-border" style="width:128px;text-align:center;"><p>14.91</p></div>
</div>
<div>
    <div style="margin:0%;padding:16px 0%;width:896px;text-align:left;"><p>
        <input id="submit" type="submit" value="Checkout&nbsp>>" />
    </p></div>
</div>
</form>
<?php else: ?>
    <div>
        <div class="basket-item" style="width:896px;text-align:center;"><p>Please add one or more items to your shopping basket.</p></div>
    </div>
<?php endif; ?>

1 个答案:

答案 0 :(得分:0)

您的网址构造不正确。

var url = '<?php echo $this -> get( '__baseuri' ); ?>shop/adjust/' + id + '/?quantity=' + q;

可能需要。

var base_uri = "<?php echo $this->get('__baseuri') ?>"; 
var url = base_uri+'shop/adjust/' + id + '/?quantity=' + q;

一旦服务器解析了URL就完全可以接受了,所以没有问题,服务器也按预期返回内容,问题只是客户端。