jquery-select2 / Copy selection from one field to another

时间:2016-11-26 18:39:35

标签: jquery-select2

I adapted some javascript to work with jquery “select 2” plugin in order to duplicate the selected fields to another part of the form. The only issue is that I am not able to get this to function with the new version of "Select 2" (v.4.0.3). Apparently some methods have been deprecated in the new version and this breaks my code.

Does anyone have insight how I may be able to duplicate form inputs with the latest version?


Here is my working example of the old select 2 version (v3.5) in jsfiddle. I would like to try and emulate this with the latest version.

Working in Select2 v3.5

http://jsfiddle.net/jinch/0oao3wo0/

Broken in Select2 v4.0.3

http://jsfiddle.net/jinch/jr3L0q4f/1/

And here is the basic code I pieced together.

$('#shipping_state').select2('val', $('#billing_state').select2("val").toString());

Any suggestions welcome. Thank you.

1 个答案:

答案 0 :(得分:2)

好的......如果有其他人需要的话,弄明白。

以下是如何将值绑定并复制到其他字段。

$("#shipping_state").val($("#billing_state").val()).trigger("change");

工作示例:

  

http://jsfiddle.net/jinch/jr3L0q4f/8/