drze7794 2015-12-22 18:47
浏览 33

如何在Wordpress管理控制面板中运行Javascript?

I'm creating a WordPress plugin that returns the value of a select box when an admin selects a value from the select box in admin panel, however, it doesn't seem to work. The alert window doesn't pop up. Here's my HTML code.

<select id="sweets">
  <option>Chocolate</option>
  <option>Candy</option>
  <option>Taffy</option>
</select>
<div id="choice"></div>

Here's my code for the plugin.

<?php
add_action( 'wp_enqueue_scripts', 'theme_name_scripts' );
function theme_name_scripts(){
    wp_register_script('selection', get_stylesheet_directory_uri() . '/js/selections.js', array('jquery') );
    wp_enqueue_script('selection');
}
?>

And lastly, here's my Javascript code.

jQuery(document).ready(function() {
    jQuery("#sweets").change(function() {
        var str = "";
        jQuery("select option:selected").each(function() {
            str += $(this).text() + " ";
        });
        jQuery( "#choice" ).text( str );
    }).change();
});

Thanks for your help.

  • 写回答

1条回答 默认 最新

  • drs7798 2015-12-22 19:21
    关注

    admin_enqueue_scripts hook is the first action hooked into the wp admin scripts actions.

    so you should use this

    <?php add_action( 'admin_enqueue_scripts', 'theme_name_scripts' ); ?>
    

    instead of

    add_action( 'wp_enqueue_scripts', 'theme_name_scripts' );
    
    评论

报告相同问题?

悬赏问题

  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?