Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
165 views
in Technique[技术] by (71.8m points)

jQuery to run after PHP load

I'm working on a site using the Big Commerce platform. The cart page is built in snippets called by PHP. I don't have access to the PHP files. What I need to do is get the value of a span tag(at a given index) and retrieve the value, I use this value to determine what my minQTY text field should be. I want my code to run after the page has been loaded. However my script isn't working on the page.

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script type="text/javascript">

$(document).load(function(){
            $("a.CustomizeItemLink");
            $changeIndex = $("a.CustomizeItemLink").index('a.CustomizeItemLink');
            $change = $("a.CustomizeItemLink").length;
            $productTestArray = ['/american-boxwood-buxus/', '/lavander-crape-myrtle-lagerstroemia-fs/']
            $productLength = $productTestArray.length;

            for(i=0, j=0; i<=$change, j<=$productLength; i++, j++){
            $productTest = "http://www.tnnursery.net"; 
            $productTestValue = $("td.blah a").get(i);                    
            $productTest = "http://www.blah.net" + $productTestArray[j]; 
            $MD = $productTestValue;
            $MS = $productTest;
            $minQTYArray = ['100','100','75','50','25','20','15'];
            $cartSpanValue = $(".productAttributes td span:eq("+i+")").text();            
            $rdTestArray2 = ['6-12','12-18',"1-2'","2-3'","3-4'","4-5'","5-6'"];
            $arr2Test = jQuery.inArray($cartSpanValue, $rdTestArray2);
            $minQTYValue = $minQTYArray[$arr2Test];
            $cartQtyValue = $(".qtyInput:eq("+i+")").val();

            if($MD != $MS){

            }
            else{    
                if($cartQtyValue >= $minQTYValue){

                }
                else{
                    if ($arr2Test == -1){
                    $cartQtyValue = $(".qtyInput:eq(" + i + ")").val('100');    
                    }
                    else{
                    $cartQtyValue = $(".qtyInput:eq(" + i + ")").val($minQTYValue);  
                    alert($cartQtyValue);
                    }
                }
            }
        }

    });
});
});
    </script>

    </td>
    <td class="ProductName" colspan="1">
        <a href="http://www.blah.net/blah-blah-blah/">Item Name1</a><table class="productAttributes" cellpadding="0" cellspacing="2">
<tr>
<td>
    <label>Plant&#8203; Size&#8203;s:</label>
</td>
<td>
    <span>12-18&#8203;&quot;</span>
</td>

Change
28500 ()

        <div style="display: none" class="WrappingOptions">
            Gift Wrapping:

            <a href="#" onclick="Cart.ManageGiftWrapping('4eea86d587825');" style="">Add</a>
            <span style="display: none">
                (<a href="#" onclick="Cart.ManageGiftWrapping('4eea86d587825');">Change</a> or <a href="cart.php?action=remove_giftwrapping&amp;item_id=4eea86d587825" onclick="return Cart.RemoveGiftWrapping();">Remove</a>)
            </span>
            <br />
            <span style="display: none">
                Gift Message:

            </span>
        </div>
    </td>
    <td align="center" class="CartItemQuantity">
        <span style="padding: 0; margin: 0;"><input type="text" size="2" name="qty[4eea86d587825]" id="text_qty_4eea86d587825" class="qtyInput quantityInput" value="50"/></span>

        <div style="">
            <a href="cart.php?action=remove&amp;item=4eea86d587825" onclick="Cart.RemoveItem('4eea86d587825'); return false;" class="CartRemoveLink">Remove</a>
        </div>
    </td>

My question is how do I set my jQuery to run after the PHP has been loaded into the page?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

First, you should restore the $(document).ready(function(){});

Second, the php runs on the server side, and thus before the page itself is loaded. You shouldn't have any worry that your javascript will run before the PHP.

Lastly, I think the reason your code isn't running may be due to syntax errors. If you've copied your code in directly then you have multiple endings to your opening function. I'm not sure if this was intentional or not but you have three sets of closing for the load function when its unnecessary.

Also, this line :

$productTestArray = ['/american-boxwood-buxus/', '/lavander-crape-myrtle-lagerstroemia-fs/']

Does not have a semi-colon ending the statement. Make these changes and see if your code begins to function properly.

EDIT: Also, in your For loop you define i and j. Why is that? I believe they will always be the same number the way you have it setup. Am I missing something?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...