简体   繁体   English

如何向新的附加选择选项显示选择选项

[英]How to display the select options to the new appended select options

I have a main select options which if the value is equal to the function the result of function will display on the sub select options.我有一个主选择选项,如果值等于函数,则函数的结果将显示在子选择选项上。 But when I append new sub select options the result wont display on the new options.但是当我附加新的子选择选项时,结果不会显示在新选项上。

<select id="system-select-1" class="form-control" data-id="1">
 <option selected>Select</option>
 <option value="test1">test1</option>
 <option value="test2">test2</option>
 <option value="test3">test3</option>
</select>

<select id="sdo-select-1" data-id="1" class="form-control sdo-select">
 <option selected>Select</option>
</select>

heres the whole code继承人整个代码

It does not show anything because you just append a sub element without filling its options.它不会显示任何内容,因为您只是附加了一个子元素而没有填写其选项。 (if i got what you mean) You need to check what main-option is selected and then fill the sub-options either before appending or just after having appended your select. (如果我明白您的意思)您需要检查选择了哪个主选项,然后在附加之前或在附加您的选择之后填写子选项。

I suggest to put these steps in separate functions, as to call them whenever you need them.我建议将这些步骤放在单独的函数中,以便在需要时调用它们。 for example when you click on "add" you trigger the function to add your sub-select and after that you trigger the function to fill its options.例如,当您单击“添加”时,您会触发该功能以添加您的子选择,然后您会触发该功能以填充其选项。 (this, obviously, is just one way to do it...) (显然,这只是一种方法......)

You can do several things to populate the newly added sub-selects.您可以做几件事来填充新添加的子选择。 The fastest would be to trigger the .change() event on the master select like this:最快的方法是在主选择上触发.change()事件,如下所示:

$(document).on('click', '#add_sdo', function(){
   i++;
   $('#dynamic_sdo').append('<div class="pr-1 salesforce_sdo"><select id="sdo-select-'+i+'" data-id="1" class="form-control sdo-select"><option selected>Select</option></select></div>');    

   $("#system-select-1").change();
});

Here is the updated JsFiddle Code这是更新的 JsFiddle 代码

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM