简体   繁体   English

无法使用jQuery覆盖Select2值

[英]Can't overwrite Select2 value with jQuery

I am using select2 in my form, and with jQuery, I am trying to set a default value to it; 我在表单中使用select2,并且在jQuery中,我试图为其设置默认值。 however whatever I tried didn't work. 但是我尝试了什么都没有用。 First, here is my html form: 首先,这是我的html表单:

<div class="form-group>
   <div class="col-xs-12">
      <div class="form-material">
          <label for="item" style="font-size: 14px; margin-bottom: 5px">Name:</label>
          <select id="itemSelect" class="js-select2" name="item" data-placeholder="Select Item..">
               <option></option>
               @foreach ($establishments as $establishment)
                    <option value="{{$item->id}}">{{ $item->name }}</option>
               @endforeach
         </select>         
     </div>
  </div>
</div>

And then, in my jquery, I am trying to write a default value on it. 然后,在我的jquery中,我试图在上面写一个默认值。

function somethingClick(id) {
   var theValueToBeSet = jQuery('#item-'+ id).html();  // logs "TestData"

   // First I tried;
   jQuery('#itemSelect').val(theValueToBeSet);

   // Secondly:
   jQuery('#itemSelect').select2('val', theValueToBeSet);

  // Thirdly:
   jQuery('#itemSelect').val(theValueToBeSet).trigger("change")
}

I get no errors in the console. 我在控制台中没有任何错误。 What am I missing/doing wrong? 我想念/做错了什么?

Check Below code you have to trigger : 检查以下代码,您必须触发:

 $("#itemSelect").val(theValueToBeSet).trigger("change");

Hope this will help you !!

Below code is working properly : 下面的代码工作正常:

("#SelectId").select2({
        placeholder: "-Select Options-"
    });
    if ($('#FirstListId').val() != 0) {
        var olddata = $("#SelectId").val();
        $.getJSON('/Home/GetSelectList/' + $('#FirstListId').val(), function (data) {
            var items;
            $.each(data, function (i, secondList) {
                items += "<option value='" + secondList.Value + "'>" + secondList.Text + "</option>";
            });
            $('#SelectId').html(items);
            $("#SelectId").val(olddata).trigger("change");
        });
    }

I hope this is what you're looking for. 我希望这是您要寻找的。

Below is the link for the example to change the value for select. 以下是用于更改select值的示例的链接。

HTML HTML

<select class="test">
    <option value="">-</option>
    <option value="1">Test 1</option>
    <option value="2">Test 2</option>
    <option value="3">Test 3</option>
</select>

Javascript 使用Javascript

$(".test option[value=3]").prop("selected","selected").trigger("change");

https://jsfiddle.net/synz/7yc96mo5/1/ https://jsfiddle.net/synz/7yc96mo5/1/

And try change the value. 并尝试更改值。 I have to add trigger because of select2 function. 由于select2函数,我必须添加触发器。

It's a little hard to tell as the variable, parameter and function names here aren't great... 很难说出来,因为这里的变量,参数和函数名称并不好...

Firstly, it's not clear what the parameter id is representing. 首先,不清楚参数id代表什么。 I suspect there is html we are not seeing as jQuery('#item-'+ id) is selecting a DOM element that has the id of '#item-'+ id , which I cannot find in your example. 我怀疑没有html,因为jQuery('#item-'+ id)选择的ID为'#item-'+ id的DOM元素,在您的示例中找不到。

Secondly, presuming that jQuery('#item-'+ id) is selecting an arbitrary option DOM element from the select, I think you are getting mixed up when using $item.id and $item.name . 其次,假设jQuery('#item-'+ id)正在从select中选择任意选项DOM元素,我认为使用$item.id$item.name时您会感到$item.name The variable theValueToBeSet is set to $item.name , however you are later using it to set the value of the select, which is $item.id . 变量theValueToBeSet设置为$item.name ,但是您稍后将使用它来设置select的 $item.id

Try amending your js to the following 尝试将您的js修改为以下内容

function somethingClick(id) {
   var theValueToBeSet = jQuery('#item-'+ id).attr('value');  // <- This line changed

   // First I tried;
   jQuery('#itemSelect').val(theValueToBeSet);

   // Secondly:
   jQuery('#itemSelect').select2('val', theValueToBeSet);

  // Thirdly:
   jQuery('#itemSelect').val(theValueToBeSet).trigger("change")
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM