jQuery 在选项列表中重新填充所选项目

jQuery repopulate the selected item in an option list

本文关键字:选项 填充 项目 新填充 列表 jQuery      更新时间:2023-09-26

已经尝试了一段时间了,没有运气。

jQuery("#choosenCause option:selected").removeAttr("selected","selected");
jQuery('#choosenCause option[value="'+json.causeID+'"]').attr("selected",true);

我正在使用上面的元素,它将selected="selected"添加到正确的元素,但不会更改用户看到的选定元素。

完整的JS在这里 - http://inspire.simplyfundraising.co.uk/wp-content/themes/inspire/assets/js/promojs.js

要设置select元素的选定选项,请使用val()

jQuery('#choosenCause').val(json.causeID);

第一个字符串中,您需要更正代码 - jQuery("#choosenCause option:selected").removeAttr("selected");第二个字符串如我所见工作正常