简体   繁体   English

Sumoselect插件不适用于动态选择下拉列表

[英]Sumoselect plugin not working with dynamically select dropdown

I am using sumoselect plugin ( http://hemantnegi.github.io/jquery.sumoselect/sumoselect_demo.html ). 我正在使用sumoselect插件( http://hemantnegi.github.io/jquery.sumoselect/sumoselect_demo.html )。

I have two select dropdowns. 我有两个选择下拉菜单。 First, one I directly added to the HTML page and the second one I am displaying dynamically using jQuery. 首先,一个我直接添加到HTML页面,第二个我使用jQuery动态显示。

sumoselect plugin is working only for the first one select dropdown but not working with the second one. sumoselect插件仅适用于第一个选择下拉列表,而不适用于第二个选择下拉列表。

Would you help me out with this issue? 您能帮我解决这个问题吗?

 $(document).ready(function() { $('.fileStatus').SumoSelect(); var wrapper = $(".appentInside .row"); //Fields wrapper var add_button = $(".click_me"); //Add button ID $(add_button).click(function(e) { //on add input button click $(wrapper).append('<select name="pp_fileStatus[]" class="fileStatus"><option disabled="" selected="">Select</option><option value="1"> One</option><option value="2">Two</option><option value="3"> Three</option></select>'); }); }); 
 <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/jquery.sumoselect/3.0.2/sumoselect.min.css"> <select name="pp_fileStatus[]" class="fileStatus"> <option disabled="" selected="">Select</option> <option value="1"> One</option> <option value="2"> Two</option> <option value="3"> Three</option> </select> <a href="javascript:void(0);" class="click_me">click me to display seocnd dropdown</a> <div class="appentInside"> <div class="row"></div> </div> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery.sumoselect/3.0.2/jquery.sumoselect.min.js"></script> 

After using @Terry answer 使用@Terry回答后

$(document).ready(function() {
    $('.fileStatus').SumoSelect();

  $('.fileStatus:first').change(function() {
    var fileStatus = $('.fileStatus option:selected').val();
    $('.fileStatus:last').val(fileStatus);
  })

  var wrapper = $(".appentInside .row"); //Fields wrapper
  var add_button = $(".click_me"); //Add button ID
  $(add_button).click(function(e) { //on add input button click
    e.preventDefault();
    var fileStatus = $('.fileStatus:last option:selected').val();
    $(wrapper).append('<select name="pp_fileStatus[]" class="fileStatus"><option disabled="" selected="">Select</option><option value="1"> One</option><option value="2">Two</option><option value="3"> Three</option></select>').find('.fileStatus').SumoSelect();
    $('.fileStatus:last').val(fileStatus);
  });

});

That is because your new .fileStatus dropdown is added after runtime , and the code $('.fileStatus').SumoSelect() has already been invoked. 这是因为您的新.fileStatus下拉列表是在运行时之后添加的,并且代码$('.fileStatus').SumoSelect()已被调用。 It is important to remember that JS is not reactive in the sense that it will automatically find new elements all the time: you will need to instantiate SumoSelect on the new <select> element that is added. 重要的是要记住,JS不会一直处于反应状态,因为它会一直自动查找新元素:您将需要在添加的新<select>元素上实例化SumoSelect。

You can simply do this by chaining .find('.fileStatus').SumoSelect() after the .append() method, because by then the new .fileStatus element will be already present in the DOM: 您只需在.append()方法之后链接.find('.fileStatus').SumoSelect()即可完成此操作,因为届时新的.fileStatus元素将已在DOM中出现:

 $(document).ready(function() { $('.fileStatus').SumoSelect(); var wrapper = $(".appentInside .row"); //Fields wrapper var add_button = $(".click_me"); //Add button ID $(add_button).click(function(e) { //on add input button click $(wrapper).append('<select name="pp_fileStatus[]" class="fileStatus"><option disabled="" selected="">Select</option><option value="1"> One</option><option value="2">Two</option><option value="3"> Three</option></select>').find('.fileStatus').SumoSelect(); }); }); 
 <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/jquery.sumoselect/3.0.2/sumoselect.min.css"> <select name="pp_fileStatus[]" class="fileStatus"> <option disabled="" selected="">Select</option> <option value="1"> One</option> <option value="2"> Two</option> <option value="3"> Three</option> </select> <a href="javascript:void(0);" class="click_me">click me to display seocnd dropdown</a> <div class="appentInside"> <div class="row"></div> </div> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery.sumoselect/3.0.2/jquery.sumoselect.min.js"></script> 

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

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