[英]How to set a value in a select2 combobox
I need to auto-fill a form that is using select2 comboboxes. 我需要自动填写使用select2组合框的表单。 The classic JS code document.getElementById('id').value = 'value';
经典的JS代码document.getElementById('id').value = 'value';
isn't working, nor do I wish to include jQuery for what should be a simple operation, unless absolutely unavoidable. 不能正常工作,也不希望将jQuery包含在内,这应该是一个简单的操作,除非绝对不可避免。 Are there any JS alternatives to this problem ? 是否有JS替代品可以解决此问题?
I have no control over the HTML, so changing the boxes isn't an option, unfortunately. 我无法控制HTML,因此不幸的是,更改框不是一个选择。
您可以将jquery
与select2一起使用,并且必须具有与以下相同的trigger('change')
:
$("#id").val(value).trigger('change');
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.