【问题标题】:how to set basic value to select box of Bootstrap Smartadmin如何设置基本值以选择 Bootstrap Smartadmin 框
【发布时间】:2015-07-16 03:32:07
【问题描述】:

我正在使用使用 SmartAdmin 主题的引导程序。

它将所有 html 结构转换为 <section><div> struct include <select><option>

所以我的输出如下所示。

<section id="00000001" class="col col-md-6">

        <label class="label">SomeLabel 
            <sapn class="cp_id_text"></sapn>            
        </label>

        <label class="select">
            <div class="select2-container form-hidden-control" id="s2id_00000001" title="" style="width: 100%;"><a href="javascript:void(0)" class="select2-choice" tabindex="-1">   <span class="select2-chosen" id="select2-chosen-9">20th Century Fox Film Corporation(00000001)</span><abbr class="select2-search-choice-close"></abbr>   <span class="select2-arrow" role="presentation"><b role="presentation"></b></span></a><label for="s2id_autogen9" class="select2-offscreen"></label><input class="select2-focusser select2-offscreen" type="text" aria-haspopup="true" role="button" aria-labelledby="select2-chosen-9" id="s2id_autogen9"></div><input type="text" class="form-hidden-control select2-offscreen" id="00000001" name="cp_id" value="" data-init-text="" inputtype="select" title="" maxlength="50" tabindex="-1">
            <script>$("#00000001").select2(selects2Option('00000002','cp_id'));</script>
        </label>    
    <script>

        $("#00000001").attr("maxlength", "50");
    </script>
</section>

由于我找不到任何&lt;select&gt; 标签,我不能使用 selected 属性来设置默认值。

我可以通过 jquery parent(), find() 等找到它,但这看起来效率不高。有什么方法可以像 selected 这样简单地处理吗?

SmartAdmin 网站说他们使用 select2 api,但他们没有提到如何将值设置为默认值。

网站:http://192.241.236.31/smartadmin/BUGTRACK/track_/documentation/index.html#!/introduction

谢谢。

【问题讨论】:

    标签: jquery twitter-bootstrap select smartadmin


    【解决方案1】:

    如果您看到select2 的演示,您会发现Programmatic access 区域,他们提供了如何在button 点击上设置值。初始化后同样可以设置默认值,如下:

    <select class="js-example-programmatic">
      <option value="one">First</option>
      <option value="two">Second</option>
      <option value="three">Third</option>
    </select>
    

    Js

    $(document).ready(function() {
         $(".js-example-programmatic").select2();
         var $example = $(".js-example-programmatic");
         $example.val("three").trigger("change");
    });
    

    DEMO HERE

    【讨论】:

    • 感谢您的回答。但是 SmartAdmin 插件将
    • 如果您提供任何形式的小提琴,那么我可以在这方面为您提供更多帮助!否则很难理解!!
    • 我还在帖子中添加了我的解决方案。它有一些我一直在努力解决的关键字。我希望这个答案可以帮助那些也面临这个问题的人:D
    • 太棒了.. :) 快乐编码.. :)
    【解决方案2】:

    我自己解决了这个问题。 @Guruparasad Rao 的回答很有帮助,但我想谈谈我是如何解决的。(因为有点不同)

    首先,我抓取了绑定到 select2 的元素。

    target.select2('destroy');
            var data = [
                        {id: 'admin', text: 'admin'}
                        , {id: 'visitor', text: 'visitor'}
                        , {id: 'buyer', text: 'buyer'}];
            target.addClass('col-md-12').select2({data : data, multiple: true});
    

    我要提到的一件事是使用idtext 键是select2 中的固定语法。该对象应包含该关键字。

    【讨论】:

      猜你喜欢
      相关资源
      最近更新 更多
      热门标签