【问题标题】:Setting multiple choices using jquery chosen plugin使用 jquery selected 插件设置多个选择
【发布时间】:2014-10-29 11:55:56
【问题描述】:

我正在使用 selected 的插件从从数据库加载的数据中选择多个项目。这已保存,然后我可能想再次加载这些选项(实际上是整个表单)以对其进行编辑并保存更改。

我可以像这样从选择组件中成功读取各种选项:

$("#meet_participants").chosen().val();

但是,如果我想设置多项选择,我正在尝试这个测试用例:

HTML 代码:

<div class="row">
  <div class="form-group">
    <label class="control-label">Participantes</label><br>
    <b>
    <select data-placeholder="Ingrese los nombres" class="chosen-select form-control" style="width:60%" multiple id="meet_participants">
    <!--Filled with all db users.-->
    <option>hola</option>
    <option>mundo</option>
    <option>cruel</option>
    <option>como</option>
    <option>estas</option>
    </select>
    </b>
    <button class="btn btn-primary"><b>Tareas pendientes</b></button>
  </div> <!--formgroup-->
</div> <!--row-->

之后我做的(根据示例)

<!-- Bootstrap core JavaScript
================================================== -->
<!-- Placed at the end of the document so the pages load faster -->
<script src="js/plugins/jquery-1.11.1.min.js"></script>
<script src="js/plugins/bootstrap.min.js"></script>  
<script src="js/plugins/chosen.jquery.js" type="text/javascript"></script>
<script type="text/javascript">
  var config = {
'.chosen-select'           : {},
'.chosen-select-deselect'  : {allow_single_deselect:true},
'.chosen-select-no-single' : {disable_search_threshold:10},
  }
  for (var selector in config) {
$(selector).chosen(config[selector]);
  }
</script>


<script>
  fillMemoForm();
  $("#meet_participants").chosen().val(["hola", "mundo", "cruel"]);
</script>

只有最后一行很重要,但我粘贴了所有内容以防万一。据我了解,这应该将所选值设置为这三个项目,但事实并非如此。我做错了什么?

【问题讨论】:

    标签: jquery jquery-chosen


    【解决方案1】:

    一个更好的解决方案而不是破坏和重新创建 dom 元素是

    $('#meet_participants').val(["hola","mundo","cruel"]).trigger('chosen:updated');
    

    【讨论】:

      【解决方案2】:

      所选插件中的 Val 不会刷新控件。像这样调用destroy

      $('#meet_participants').chosen('destroy').val(["hola","mundo","cruel"]).chosen();
      

      $('#meet_participants').chosen();
      
      $('#btn1').click(function() {
        $('#meet_participants').chosen('destroy').val(["hola", "mundo", "cruel"]).chosen();
      });
      
      $('#btn2').click(function() {
        $('#meet_participants').chosen('destroy').val(["estas", "como"]).chosen();
      });
      body {
        padding: .5rem;
      }
      <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
      <script src="http://harvesthq.github.io/chosen/chosen.jquery.js"></script>
      <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/js/bootstrap.min.js"></script>
      <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css" rel="stylesheet" />
      <link href="http://harvesthq.github.io/chosen/chosen.css" rel="stylesheet" />
      
      <form role="form">
        <div class="form-group">
          <select data-placeholder="Ingrese los nombres" class="chosen-select form-control" multiple id="meet_participants" style="width:50%">
            <option>hola</option>
            <option>mundo</option>
            <option>cruel</option>
            <option>como</option>
            <option>estas</option>
          </select>
        </div>
      
        <div class="form-group">
          <button id="btn1" class="btn">set ["hola", "mundo", "cruel"]</button>
        </div>
        <div class="form-group">
          <button id="btn2" class="btn">set ["estas", "como"]</button>
        </div>
      
      </form>

      【讨论】:

        【解决方案3】:

        如果您要在选定的多选中更新逗号分隔值,请先将其转换为数组,然后触发该数组的更新。

        var meet_participants = "hola,mundo,cruel";  
        var array = meet_participants.split(',');
        $('#meet_participants').val(array).trigger('chosen:updated');
        

        瞧!

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2018-01-13
          • 2016-12-01
          • 1970-01-01
          • 2013-05-11
          • 1970-01-01
          • 2011-06-02
          • 2011-05-16
          • 2011-01-14
          相关资源
          最近更新 更多