I have a function that loops through data and dynamically creates options inside a select. The problem is I only want one "please select" option to show up, not one for each time the loop runs. So basically, if this option already exists don't add it again. I tried this code but it will not work, it doesn't show this option at all.
if ($("#level"+num+" option[value = 'base']").length < 0) {
$("#level"+num ).prepend($("<option></option>")
.attr("value", "base")//.id )
.text( "Please select an option" )
);
};
Length will never be < 0. It will be 0 or greater...
if ($("#level"+num+" option[value = 'base']").length == 0) {
$("#level"+num ).prepend($("<option></option>")
.attr("value", "base")//.id )
.text( "Please select an option" )
);
};
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.