[英]Set the first option select2
I want to set the first position to select2 dropdown by default, I have tried this but It doens´t work:我想默认将第一个位置设置为 select2 下拉列表,我已经尝试过了,但它不起作用:
$('#mylist').val($('#mylist option:first-child').val()).trigger('change');
Other way that I tried;我尝试过的其他方式;
$('#mylist').val(1);
But the problem is I don´t know what value is, because it is depend from a query and It will not always be the same value.但问题是我不知道值是什么,因为它依赖于查询并且它并不总是相同的值。
I did not set the dropdown values from the HTML, but it is an input hidden and the values are loaded in a query我没有从 HTML 中设置下拉值,但它是一个隐藏的输入,并且值是在查询中加载的
I hope that anyone can help me我希望任何人都可以帮助我
Regards!问候!
如果您使用 Select2 4.x 只需触发change.select2
$('#mylist').val(1).trigger('change.select2');
please try with this:请试试这个:
$('#yourSelect2').val($('#yourSelect2 option:eq(1)').val()).trigger('change');
the value from eq() depends of the position that you want to select eq()的值取决于您要选择的位置
这对我有用:
$('#mylist option:eq(0)').prop('selected',true);
请这样做
$('select#mylist').val(1).select2();
It's better to use attribute specifiers and set that element's selected
prop to true
like so:最好使用属性说明符并将该元素的
selected
属性设置为true
,如下所示:
$('option[value="op2"]').prop('selected', true);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <select id="list"> <option value="op1">Option 1</option> <option value="op2">Option 2</option> <option value="op3">Option 3</option> <option value="op4">Option 4</option> </select>
Then change op2
to whatever the value
attribute of the desired default option is.然后将
op2
更改为所需默认选项的value
属性。
As you may know Select2 need data in particular format only [id="id_value",text="data_linked_to_id"]
您可能知道 Select2 只需要特定格式的数据
[id="id_value",text="data_linked_to_id"]
Lets consider you want to select first item ,you should have its Id which is used by select2 to list all items.(let here first id be 1)让我们考虑您要选择第一个项目,您应该有它的 ID,select2 使用它来列出所有项目。(让这里的第一个 id 为 1)
$('#selectElementId').val("1");
$('#selectElementId').val("1"); $('#selectElementId').trigger('change');
$('#selectElementId').trigger('change');
If want to set multiple items selected then pass array of ids.如果要设置选择的多个项目,则传递 id 数组。
ids=["1","3","4"] ids=["1","3","4"]
$("#selectElementId").val(ids);
$("#selectElementId").val(ids);
$("#selectElementId").trigger('change');$("#selectElementId").trigger('change');
**OR**
$("#selectElementId").val(["1","3","4"]);
$("#selectElementId").val(["1","3","4"]);
$("#selectElementId").trigger('change');$("#selectElementId").trigger('change');
Step 1: Select first option of your select(s) - works not only for select2第 1 步:选择您的选择的第一个选项 - 不仅适用于 select2
$('#myForm').find('select').find('option:eq(0)').attr('selected','selected');
Step 2: Trigger select2 change第 2 步:触发 select2 更改
$('#myForm select').trigger('change.select2');
this work for me :这对我有用:
$("#mylist").val($("#mylist option:first").val()).trigger('change');
juste remove -child
只是删除
-child
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.