How do I change selected value of select2 dropdown with JqGrid?

后端 未结 16 865
遥遥无期
遥遥无期 2020-11-27 10:27

I\'m using Oleg\'s select2 demo, but I am wondering whether it would be possible to change the currently selected value in the dropdown menu.

For example, if the fou

相关标签:
16条回答
  • 2020-11-27 10:54

    This should be even easier.

    $("#e1").select2("val", ["View" ,"Modify"]);
    

    But make sure the values which you pass are already present in the HTMl

    0 讨论(0)
  • 2020-11-27 10:56

    If you want to add new value='newValue' and text='newLabel', you should add this code:

    1. Add new Option to <select>:

      var opt = "<option value='newValue' selected ='selected'>" + newLabel+ " </option>";
      $(selLocationID).html(opt);
      
    2. Trigger <select> change to selected value:

      $(selLocationID).val('newValue').trigger("change");
      
    0 讨论(0)
  • 2020-11-27 10:57
    this.$("#yourSelector").select2("data", { id: 1, text: "Some Text" });
    

    this should be of help.

    0 讨论(0)
  • 2020-11-27 10:58

    Looking at the select2 docs you use the below to get/set the value.

    $("#select").select2("val"); //get the value
    $("#select").select2("val", "CA"); //set the value
    

    @PanPipes has pointed out that this has changed for 4.x (go toss him an upvote below). val is now called directly

    $("#select").val("CA");

    So within the loadComplete of the jqGrid you can get whatever value you are looking for then set the selectbox value.

    Notice from the docs

    Notice that in order to use this method you must define the initSelection function in the options so Select2 knows how to transform the id of the object you pass in val() to the full object it needs to render selection. If you are attaching to a select element this function is already provided for you.

    0 讨论(0)
  • 2020-11-27 11:02

    you can simply use the get/set method for set the value

    $("#select").select2("val"); //get the value
    $("#select").select2("val", "CA"); //set the value
    

    or you can do this:

    $('#select').val("E").change(); // you must enter the Value instead of the string
    
    0 讨论(0)
  • 2020-11-27 11:03

    For V4 Select2 if you want to change both the value of the select2 and the text representation of the drop down.

    var intValueOfFruit = 1;
    var selectOption = new Option("Fruit", intValueOfFruit, true, true);
    $('#select').append(selectOption).trigger('change');
    

    This will set not only the value behind the scenes but also the text display for the select2.

    Pulled from https://select2.github.io/announcements-4.0.html#removed-methods

    0 讨论(0)
提交回复
热议问题