How to remove only unique options in the selection selection?

I have a drop down menu of choice. I add parameter values ​​dynamically from the backend, but when the same values ​​arrive, there are duplicates in the drop-down list. I want to have only unique values ​​in the drop-down list. How to check if an existing value exists before adding it to the drop-down list?

I add a value something like this:

$("#pid").append('<option value="'+strSplit[0]+'"selected="">'+strSplit[0]+'</option>'); $("#pid").selectpicker("refresh"); 
+5
source share
1 answer

Use the selector to search for an existing option with the same value. If it does not exist, add it.

 if ($("#pid option[value=" + strSplit[0] + "]").length == 0){ $("#pid").append('<option value="'+strSplit[0]+'"selected="">'+strSplit[0]+'</option>'); $("#pid").selectpicker("refresh"); } 
+4
source

Source: https://habr.com/ru/post/1269499/


All Articles