我正在使用引导程序,它使用SmartAdmin主题。
它将所有html结构转换为<section><div>结构,包括<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>由于找不到任何<select>标记,所以无法使用选定的属性设置默认值。
我可以通过jQueryparent()、find()等找到它,但这看起来并不有效。有什么方法可以像selected这样简单地处理吗?
SmartAdmin站点说他们使用select2 api,但是他们没有提到如何将值设置为默认值。
网站:/documentation/index.html#!/introduction
谢谢。
发布于 2015-05-06 07:17:10
如果您看到了select2演示,您将发现程序存取区域,在那里,他们给出了如何在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");
});这里的演示
发布于 2015-07-02 01:29:40
我自己解决了这个问题。@Guruparasad的回答很有帮助,但我想谈谈我是如何解决问题的。
首先,我抓取了绑定到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});我想提到的一件事是使用id和text键是select2中的固定语法。对象应该包含该关键字。
https://stackoverflow.com/questions/30069239
复制相似问题