Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/86.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript jQuery“selected”:与选项标记中的“selected”属性冲突_Javascript_Jquery_Jquery Chosen - Fatal编程技术网

Javascript jQuery“selected”:与选项标记中的“selected”属性冲突

Javascript jQuery“selected”:与选项标记中的“selected”属性冲突,javascript,jquery,jquery-chosen,Javascript,Jquery,Jquery Chosen,由于与jQuery插件存在“冲突”,我无法使jQuery代码段正常工作。我在用Wordpress以防万一 我想在几个标记中设置所选属性,使用 jQuery( document ).ready(function() { var arr = ["1", "2"]; jQuery("#location option").each(function () { if (jQuery.inArray(jQuery(this).val(), arr) != -1) { jQuery(t

由于与jQuery插件存在“冲突”,我无法使jQuery代码段正常工作。我在用Wordpress以防万一

我想在几个标记中设置所选属性,使用

jQuery( document ).ready(function() {
var arr = ["1", "2"];
jQuery("#location option").each(function () {
    if (jQuery.inArray(jQuery(this).val(), arr) != -1) {
        jQuery(this).prop('selected', true);
    };
});
});
html输出

我觉得我在这里提供了一些细节来解决这个问题,但是我不确定在这里发布什么额外的代码

当禁用不将所选插件CHOIDED.jquery.min.js排队时,一切正常。尽管如此,我还是希望所选的插件能够正常工作。

您的代码确实可以正常工作,尽管它是按照您调用它的顺序进行的,这可能会有点磕磕绊绊。正如这一点所证明的那样

functions.php myscript.js
控制台中有任何错误吗?它在这里可以使用,但需要注意的是,在运行.each方法后初始化.select。在控制台中没有错误console@MackieeE,看起来很棒!尽管如此,我还是不知道如何在我的.each方法之后初始化.select。我是否需要在我自己的脚本之后将所选脚本排队,即包含依赖项?@mackieee,谢谢。不过,我的订单似乎有问题。我试图通过替换jQuerylocation.selected来追踪问题;如果选择了jQuerylocation.if类型!='未定义的“{alert error;}”将弹出警报窗口。这意味着jQuerylocation.Selected在右前的某个位置初始化?有没有可能找到位置?@SPi抱歉,我在typeof的评论中犯了一个错误,应该是console.log typeof jQuery.fn.selected;。我错过了.fn。第x部分,通常脚本在wp_头加载,你确定你也在模板中调用它吗?我在页面上使用检查,两个脚本都出现了。此外,还选择了console.log typeof jQuery.fn;我想我找到了导致我的问题的原因:在另一个文件.js中有:function initselected{$'select'.eachfunctionindex{$this.selected{disable_search_threshold:1};}}@SPi Ah!所以这可能有很多原因,然后x道歉,我不能更快地指出我自己或什么;希望我在这方面有所帮助!:
 <select name="location[]" multiple="multiple" id="location" class="postform" style="display: none;">
        <option value="-1">Select a location</option>
        <option class="level-0" value="1">Location A</option>
        <option class="level-0" value="2">Location B</option>
        <option class="level-0" value="3">Location C</option>
    </select>
<?php 
    /**
     *  Load:
     *    css; chosen; jquery.
    **/
    function wordpress_scripts() {

        wp_enqueue_style( 
                   'chosen.css', 
                   get_stylesheet_uri() 
                 );

        wp_enqueue_script( 
                   'chosen.js', 
                   get_template_directory_uri() . '/js/chosen.js', 
                   array('jquery')
                 );

        wp_enqueue_script( 
                   'myscript.js', 
                   get_template_directory_uri() . '/js/myscript.js', 
                   array('jquery')
                 );
    }

    add_action( 'wp_enqueue_scripts', 'wordpress_scripts' );
?>
<script>
    jQuery(document).ready(function(){
         var arr = ["1", "2"];
         jQuery("#location option").each(function () {
             if (jQuery.inArray(jQuery(this).val(), arr) != -1) {
                 jQuery(this).prop('selected', true);
             };
         });

         if ( typeof jQuery.fn.chosen !== 'undefined' )
             jQuery("#location").chosen();
    });
</script>