简体   繁体   English

select2 不是函数

[英]select2 is not a function

I have this html我有这个 html

 <div class="input-box">
        <select id="billing:country_id" class="validate-select" name="billing[country_id]" title="Country">
            <option value=""> </option>
        </select>
    </div>
    <div class="input-box">
        <select id="billing:region_id" class="validate-select" name="billing[region_id]" title="State">
            <option value=""> </option>
        </select>
    </div>

<script type="text/javascript">
    jQuery("#billing\\:country_id").select2({
        placeholder: "Select a Country"
    });

    jQuery("#billing\\:region_id").select2({
        placeholder: "Select State"
    });

    jQuery("#billing\\:country_id").change(function(){
        jQuery("#billing\\:region_id").select2('destroy'); 

        jQuery("#billing\\:region_id").select2({
            placeholder: "Select a State"
        });
    });
</script>

but when I try to change the country dropdown I have this error:但是当我尝试更改国家/地区下拉列表时,出现此错误:

TypeError: jQuery(...).select2 is not a function TypeError: jQuery(...).select2 不是函数

jQuery("#billing\\:region_id").select2('destroy'); jQuery("#billing\\:region_id").select2('destroy');

I didn't see any error except that your whole function should be encapsulated inside jquery.To show demo, i just added 3 options.我没有看到任何错误,只是你的整个函数应该封装在 jquery 中。为了显示演示,我只添加了 3 个选项。

 $(function(){ $("#billing\\\\:country_id").select2({ placeholder: "Select a Country" }); $("#billing\\\\:region_id").select2({ placeholder: "Select State" }); $("#billing\\\\:country_id").change(function(){ $("#billing\\\\:region_id").select2('destroy'); $("#billing\\\\:region_id").select2({ placeholder: "Select a State" }); }); });
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <link href="https://rawgit.com/select2/select2/master/dist/css/select2.min.css" rel="stylesheet"/> <script src="https://rawgit.com/select2/select2/master/dist/js/select2.js"></script> <div class="input-box"> <select id="billing:country_id" class="validate-select" name="billing[country_id]" title="Country"> <option value=""> </option> <option value="1">option 1 </option> <option value="2">option 2 </option> <option value="3">option 3 </option> </select> </div> <div class="input-box"> <select id="billing:region_id" class="validate-select" name="billing[region_id]" title="State"> <option value=""> </option> <option value="1">option 1 </option> <option value="2">option 2</option> <option value="3">option 3</option> </select> </div>

Here is the JsFiddle Link .这是JsFiddle 链接

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM