0

This is my Razor view.

@Html.TextBoxFor(a => a.wellModel.APINumber, new { id = "apiselect2" })

This is my script to get the selected value from select2:

$(document).ready(function () {
            $("#apiselect2").on("select2-selecting", function () {
                alert($("#apiselect2").select2('data').text);
           });
});

For some reason it only shows it to me once and if I change from let's say selected value 777 to 321 no alert is displayed.

I also tried to change this to:

$("#apiselect2").on("change", function () { ...

Still nothing. Really appreciate any help on this.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129

1 Answers1

0

Apparently you have identical ids for every item, so the change event won't be fired if you select item with the same id, because id of newly selected item is identical.

To resolve the problem, add different ids to items.

Wirone
  • 3,304
  • 1
  • 29
  • 48
Andrey
  • 1