簡體   English   中英

WordPress的+ jQuery的(在functions.php內部)不起作用

[英]Wordpress + jquery (inside functions.php) not working

我已經完成了將此腳本添加到\\ mythemes \\ functions.php中

// FGI CODE started

function my_scripts_method() {
// register your script location, dependencies and version
   wp_register_script('custom_script',
   get_template_directory_uri() . '/js/javascript_.js',
   array('jquery'),
   '1.0' );
 // enqueue the script
  wp_enqueue_script('custom_script');
  }
add_action('wp_enqueue_scripts', 'my_scripts_method');

wp_enqueue_script('jquery');
// FGI CODE ended

還有我的jQuery腳本,位於\\ mythemes \\ js \\ javascript_.js下

(function($) {
$(document).ready(function() {

 $("#pilih_bulan").change(function() {

 alert('a');

 });

})
})(jQuery);

實際上,我的目的是使ID為#pilih_bulan的組合框每次用戶每次更改選擇(下拉項)時始終觸發一些ajax函數。

上面的代碼(使用警報代碼)工作正常。 不幸的是,當我更改為以下代碼時,它根本無法工作。 有任何想法嗎?

(function($) {
$(document).ready(function() {

 $("#pilih_bulan").change(function() {

 $.ajax({
  method: "POST",
  url: "./another_processor.php",
  data: { name: "John", location: "Boston" }
})
  .done(function( msg ) {
    alert( "Data Saved: " + msg );
  });

 });

})
})(jQuery);

使用以下腳本,以及為什么使用wp_enqueue_script('jquery'); 請檢查您的源代碼jquery必須已經添加

<?php

function my_scripts_method() {
    wp_enqueue_script(
        'custom-script',
        get_stylesheet_directory_uri() . '/js/custom_script.js',
        array( 'jquery' )
    );
}

add_action( 'wp_enqueue_scripts', 'my_scripts_method' );

?>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM