Wordpress:将PHP站点与jQuery集成

I am integrating a PHP Site into my Wordpress Installation with a Plugin. This is working fine. Now I tried to update a dropdown list with data from a mySQL DB using a "jQuery":

<script type="text/javascript" src="js/jquery.js"></script>
    <script type="text/javascript">
    jQuery(document).ready(function() {
        
        jQuery("#parent_cat").change(function() {
            jQuery(this).after('<div id="loader"><img src="img/loading.gif" alt="loading subcategory" /></div>');
            jQuery.get('loadsubcat.php?parent_cat=' + $(this).val(), function(data) {
                jQuery("#sub_cat").html(data);
                jQuery('#loader').slideUp(200, function() {
                    jQuery(this).remove();
                });
            }); 
        });

    });
    </script>

the html part:

form method="get">
    <label for="category">Hauptkategorie</label>
    <select name="parent_cat" id="parent_cat">
        <?php while($row = mysql_fetch_array($query_parent)): ?>
        <option value="<?php echo $row['cat_id']; ?>"><?php echo $row['category']; ?></option>
        <?php endwhile; ?>
    </select>
    <br/><br/>

    <label>Unterkategorie</label>
    <select name="sub_cat" id="sub_cat"></select>
</form>

When calling the page with the "normal" (www.mysitename.com/wp-content/themes/theme/upload.php) URL then everything is working fine. But when I call the page from the "Wordpress - URL" (www.mysitename.com/upload) then the second dropdown list will never be populated.

Do I have to call the routine below alltough I am integrating a php side into my wordpress application?

<?php
function my_scripts_method() {
    wp_deregister_script( 'jquery' );
    wp_register_script( 'jquery', 'http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js');
    wp_enqueue_script( 'jquery' );
}    

add_action('wp_enqueue_scripts', 'my_scripts_method');
?>
 

Kind Regards,

Stefan

</div>
dongyi0210
dongyi0210 亲爱的卡特琳,你绝对是对的!我必须在所有链接上使用绝对路径。我改变了,现在一切正常!非常感谢!亲切的问候,斯特凡
5 年多之前 回复
dongxin5429
dongxin5429 如果您使用<scripttype=“text/javascript”src=“js/jquery.js”></script>,可能是由于错误的jquery文件路径,您可以检查控制台中是否有任何错误消息(铬检查员/firebug)
5 年多之前 回复
duannian7116
duannian7116 当我从wordpress调用php文件时,第二个下拉列表将不会被填充,尽管我直接调用页面(通过URL)然后,一切正常。Wordpress不会给我任何错误!我还没有打电话给“wp_enqueue_script”?我是否必须调用它-尽管我的php页面只是从wordpress调用而不是真正的一部分?亲切的问候,斯特凡
5 年多之前 回复
douwei1174
douwei1174 什么错误被抛出?为什么要在wp_enqueue_script()之上的脚本标记中包含jQuery?
5 年多之前 回复

1个回答

    <script type="text/javascript" src="http://domainname.com/wp-content/themes/lawyerplus/js/jquery.js"></script>
<script type="text/javascript">
jQuery(document).ready(function() {
    
    jQuery("#parent_cat").change(function() {
        jQuery(this).after('<div id="loader"><img src="http://www.domainname.com/wp-content/themes/lawyerplus/img/loading.gif" alt="loading subcategory" /></div>');
        jQuery.get('http://domainname.com/wp-content/themes/lawyerplus/loadsubcat.php?parent_cat=' + $(this).val(), function(data) {
            jQuery("#sub_cat").html(data);
            jQuery('#loader').slideUp(200, function() {
                jQuery(this).remove();
            });
        }); 
    });

});
</script>

To run the jquery the links have to be absolute!

</div>
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐