使用jQuery,为Select2选择下拉列表赋值不起作用

时间:2017-10-24 15:51:31

标签: javascript jquery jquery-select2

我有一个复选框,选中后,为下拉选择框指定一个值。问题是,选择下拉列表设计为var builder = new WebHostBuilder(); builder = builder.UseKestrel(); builder = builder.UseConfiguration(config); builder = builder.UseContentRoot(Directory.GetCurrentDirectory()); builder = builder.UseStartup<Startup>(); var host = builder.Build(); ,并且为select的值分配不起作用。

我已经找了一段时间的解决方案但是没有一个明确的解决方案解释。我希望,你可以帮助我解决问题。提前感谢您的建议。

以下是没有Select2&#39;它如何运作的工作示例。 fiddle link

以下是使用Select2&#39;的代码段和fiddle&#39; &#39;不工作的版本&#39;例如:

&#13;
&#13;
Sub TestListBox()
    Dim lstRw As Integer
    Dim vertical As String
    Dim i As Integer
    Dim j As Integer
    lstRw = ThisWorkbook.Sheets(1).Range("A" & Rows.Count).End(xlUp).Row
    ListBox1.ColumnCount = 7
    ListBox1.Clear
    For i = 2 To lstRw
        If ComboBox4.Value = ThisWorkbook.Sheets(1).Range("A" & i).Value Then
            Me.ListBox1.AddItem ThisWorkbook.Sheets(1).Range("A" & i).Value
            j = 1
            Me.ListBox1.List(ListBox1.ListCount - 1, j) = ThisWorkbook.Sheets(1).Range("B" & i).Value
            j = j + 1
            Me.ListBox1.List(ListBox1.ListCount - 1, j) = ThisWorkbook.Sheets(1).Range("C" & i).Value
            j = j + 1
            Me.ListBox1.List(ListBox1.ListCount - 1, j) = ThisWorkbook.Sheets(1).Range("D" & i).Value
            j = j + 1
            Me.ListBox1.List(ListBox1.ListCount - 1, j) = ThisWorkbook.Sheets(1).Range("E" & i).Value
            j = j + 1
            Me.ListBox1.List(ListBox1.ListCount - 1, j) = ThisWorkbook.Sheets(1).Range("F" & i).Value
            j = j + 1
            Me.ListBox1.List(ListBox1.ListCount - 1, j) = ThisWorkbook.Sheets(1).Range("G" & i).Value
        End If
    Next i
End Sub
&#13;
Select2
&#13;
&#13;
&#13;

2 个答案:

答案 0 :(得分:1)

您可以触发name事件以强制刷新选择值:

change

答案 1 :(得分:1)

为此,你必须使用select2 change事件调用/刷新,只需在分配新值时将其添加到代码中:

 $("select#appearance").val("green").trigger('change');

所以会像

一样

$(".js-example-basic-single").select2();

$(function() {
   $("#geStd").click(function() {
      if ($(this).attr('id') === "geStd") {
        $("select#appearance").val("green").trigger('change');
      } else {
         $("#geStd").prop('checked', false);
      }
   });
   $("select").change(function() {
      if (this.value !== "green") {
         $("#geStd").prop('checked', false);
      }
   });
});
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" />
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.4/css/select2.css" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.4/js/select2.full.min.js"></script>

<div class="col-md-9">
   <input id="geStd" type="checkbox">
   <label for="geStd">Set to standard</label>
</div>
<div class="row">
   <label for="" class="col-md-4">Appearance</label>
   <div class="col-md-8">
      <select id="appearance" name="" class="form-control js-example-basic-single">
         <option value="">Select one</option>
         <option value="red">Red</option>
         <option value="green">Green</option>
      </select>
   </div>
</div>