简体   繁体   English

Select2 Widge重置所选值

[英]Select2 Widge Reset Selected Value

How to reset selected value in yii2 widget select2 using jQuery or JavaScript? 如何使用jQuery或JavaScript重置yii2小部件select2中的选定值? I've tried to use $("#select2id").val("").trigger("change"); 我试图使用$("#select2id").val("").trigger("change"); and it show the placeholder, but the first item is always automatically selected. 并显示占位符,但始终自动选择第一项。

EDIT: I'm using this widget for make dependency dropdown. 编辑:我正在使用此小部件进行依赖性下拉列表。 the widget that the first item always automatically selected is the dependent widget. 始终自动选择第一项的小部件是从属小部件。

预期结果

You should do it the select2 way: 您应该采用select2方法:

$("#select2id").select2("val", "", true);

With the first two parameters you assign an empty string as a value and the last one is for triggering the change. 使用前两个参数,您可以将一个空字符串指定为一个值,最后一个参数用于触发更改。

I've solved the problem. 我已经解决了问题。 That code is actually working. 该代码实际上是有效的。 The order of the code that make the problem. 导致问题的代码顺序。 So, be sure to check the code order. 因此,请务必检查代码顺序。

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

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