JQuery 不会在 AJAX GET 调用 (PHP) 之后替换内容


JQuery not replacing content after AJAX GET call (PHP)

更改输入值后,内容将替换为微调器和"请稍候..."消息,一旦得到来自 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; ?>

您的网址构造不正确。

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是完全可以接受和正确的,所以那里没有问题,服务器也按预期返回内容,问题仅在客户端。