简体   繁体   English

如何使用动态添加的选项更改所选选项?

[英]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.

相关问题 如何动态删除选定的选项 - How do I remove a selected option dynamically 如何使用 JavaScript 更改 HTML 选定选项? - How do I change an HTML selected option using JavaScript? 如何使用javascript更改选项标签上的“选定”属性 - How do I change the “selected” attribute on an option tag with javascript 选择一个选项后,我想更改其他 select 标签选项 - When an option selected I wanna change the other select tags options 如何将选定的ng-options选项的名称更改为自定义名称? - How can i change the selected ng-options option's name to something custom? 在动态添加的字段上禁用所选选项 - Disable selected options on dynamically added fields 动态更改默认选择的选项 - change default selected option dynamically 如何在具有相同选项的其他选择中禁用、隐藏或删除所选选项? - How do I disable, hide, or remove selected option in other selects with the same options? 如何根据在“下拉列表 A”上选择的选项禁用“下拉列表 B”的某些选项? - How do I disable certain options of 'Dropdown B' depending on an option selected on 'Dropdown A'? 如何使用 jQuery、JavaScript 和 HTML 动态设置下拉列表的选定选项? - How do I dynamically set the selected option of a drop-down list using jQuery, JavaScript and HTML?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM