[英]How do I change the selected option with dynamically added options?
I have a php page with some javascript in it. 我有一个带有一些javascript的php页面。 There are some select elements with static options and some select elements I append options to.
有一些带有静态选项的select元素和一些我附加选项的select元素。 For the static options I can change the selected option using the code below.
对于静态选项,我可以使用下面的代码更改所选选项。 But for the dynamically added options I cannot change the selected option.
但是对于动态添加的选项,我无法更改所选选项。 I have feeling I am not selecting the element correctly since it was not on the DOM originally.
我觉得我没有正确选择元素,因为它最初不在DOM上。
I can dynamically add options to a select
with this 我可以动态地添加选项的
select
与此
feedback_sub_type.append(new Option(user.value, user.value));
However I cannot change the selected option with this 但是我无法用此更改所选选项
$('#feedback_sub_type').val('<?php echo $feedback_sub_type ?>');
I got it to work. 我得到了它的工作。 Not exactly what I was looking for but it will do.
不完全是我想要的,但它会做。
if(user.value != selected_type){
feedback_type.append(new Option(user.value, user.value));
}
else{
feedback_type.append(new Option(user.value, user.value, true, true));}```
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.