代码之家  ›  专栏  ›  技术社区  ›  Vikas Hire

未捕获的类型错误:$(…)。SelectBox不是函数

  •  0
  • Vikas Hire  · 技术社区  · 7 年前

    我正在使用下面的代码从下拉列表中选择选项,但我得到:

    Uncaught TypeError: $(...).selectBox is not a function.
    

    在控制台中。我要用 jquery-selectBox 是的。

    我的代码:

    <script>
        $(document).ready(function() {
            $("SELECT").selectBox();
            $("SELECT").selectBox('settings', {
                'menuTransition': 'fade',
                'menuSpeed': 'fast'
            });
        });
    </script>
    

    在body标签中,我得到一个select字段:

    <select class="selectBox">
        <option value="0">Login Type</option>
        <option value="1">Admin</option>
        <option value="2">Customer</option>
    </select>
    

    我在代码中包含了所有的javascript源代码,但它仍然给了我错误。有什么解决办法吗?

    1 回复  |  直到 7 年前
        1
  •  1
  •   user7637745 Alaa    7 年前

    为了使用 jQuery selectBox ,只需将其正确加载到页面上(例如 via CDN )中。

    $(document).ready(function() {
      $("select").selectBox();
      $("select").selectBox('settings', {
        'menuTransition': 'fade',
        'menuSpeed': 'fast'
      });
    });
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery.selectbox/1.2.0/jquery.selectBox.js"></script>
    
    <select class="selectBox">
      <option value="0">Login Type</option>
      <option value="1">Admin</option>
      <option value="2">Customer</option>
    </select>

    注意

    为了更有效地使用标记,在本例中,使用元素的 class 属性及其价值 selectBox 使用jquery选择它,例如:

    您的标记:

    <select class="selectBox">
    

    通过以下方式选择:

    $(".selectBox").selectBox();