2015-09-01 33 views
0

我有我与WordPress使用一些不同的jQuery的脚本工作,除了这个其他所有工作的罚款:
更新添加一个小提琴:
jsfiddle.net/zq43e9ed
只有选项A和选项B可以一起选择,如果选择选项AA或BB,则A和B将被取消选择。这正常工作对这个小提琴,但不是当我实现它WordPress的jQuery脚本没有在WordPress

下面是我在AJS文件中使用的WordPress代码:

jQuery("#silhouette-form").on("click", ".selection", function() { 
    var $this = $(this); 
    var all = $("#silhouette-form").find(".selection").not($this); 
    var val = $this.val(); 

    if ($this.prop("checked") === true) { 
     all.filter(function() { 
      return $(this).val() != val; 
     }).prop("checked", false); 
    } 
}); 

而且我已经列入的functions.php:

add_action('wp_enqueue_scripts', 'add_my_script'); 
function add_my_script() { 
wp_enqueue_script(
    'customize-two-select', 
    get_template_directory_uri() . '/js/customize-two-select.js', 
    array('jquery') 
); 
} 
+0

你能描述有问题的jQuery脚本有什么问题吗? –

+0

Chrome开发人员工具控制台中是否有错误?文件是否在浏览器中正确加载?另外,在wordpress中,jQuery被设置为noConflict模式,所以jQuery的$快捷方式不可用。 https://codex.wordpress.org/Function_Reference/wp_enqueue_script#jQuery_noConflict_Wrappers – ucheng

+0

我已经添加了一个小提示,以便使用以下脚本:https://jsfiddle.net/zq43e9ed/ 只有选项A和选项B可以一起选择,如果选择AA或BB选项,则A和B将被取消选中。 这在这个小提琴上正常工作,但不是当我在wordpress上实现它时 – vermit25

回答

1

如果你的jQuery与其他jQuery相冲突,你可以使用这种技术。

<script type="text/javascript">var jQuery_1_10_2 = $.noConflict(true);</script> 

<script> 
jQuery_1_10_2("#silhouette-form").on("click", ".selection", function() { 
var $this = $(this); 
var all = $("#silhouette-form").find(".selection").not($this); 
var val = $this.val(); 

if ($this.prop("checked") === true) { 
    all.filter(function() { 
     return $(this).val() != val; 
    }).prop("checked", false); 
} 
}); 
</script> 
1

您不能使用$快捷这样,如果你使用附带的WordPress(包含在noConflict模式)的默认jQuery的。你要么需要使用jQuery无处不在,或使用包装的$快捷方式分配给它:

(function($){ 
    $("#silhouette-form").on("click", ".selection", function() { 
     var $this = $(this); 
     var all = $("#silhouette-form").find(".selection").not($this); 
     var val = $this.val(); 

     if ($this.prop("checked") === true) { 
      all.filter(function() { 
       return $(this).val() != val; 
      }).prop("checked", false); 
     } 
    }); 
})(jQuery); 

你可以阅读更多关于WordPress的in the Codex使用jQuery。