简体   繁体   English

如何使用 jquery 在不丢失样式和插件(select2)的情况下刷新 div

[英]How to refresh a div without losing the style and plugin (select2) using jquery

In the HTML I am trying to reload this div below;在 HTML 我试图在下面重新加载这个 div;

           <select name="test[]" id="test" multiple required
                   class="select2">
               @foreach($tests as $s)
                     <option value="{{ $s->id }}" {{ ( in_array($s->id, $tests) ) ? 'selected="selected"' : '' }}>
                             {{ $s->value }}
                      </option>
                @endforeach
           </select>

I am using a script to reload the content but losing the select2 functionality for the div here is the snippet I am using to refresh the tag我正在使用脚本重新加载内容,但丢失了 div 的 select2 功能,这是我用来刷新标签的片段

 $( "#test12" ).load(window.location.href + " #test" );

you need to re-initiate the select2 plugin after every refresh of your div element.您需要在每次刷新 div 元素后重新启动 select2 插件。

because select2 gets bind via script once you called .select2() on your select tag, and whenever you refresh the div element in which your select tag is there, the binding will be removed, and it will became normal default select dropdown.因为一旦您在 select 标记上调用.select2() ,select2 就会通过脚本绑定,并且每当您刷新包含 select 标记的 div 元素时,绑定将被删除,它将变为正常的默认值 Z999439Z415dropdown。 So for binding it again you have to rebind it.因此,要再次绑定它,您必须重新绑定它。

You can try these ways to do it.您可以尝试这些方法来做到这一点。 $("#test").select2(); $("#test").select2(); or $("#test").select2("refresh");$("#test").select2("refresh");

I am new here though;不过我是新来的; all these didnt work for me.所有这些对我都不起作用。

I had to get the value of the $( "#test12" ) and push the new created object from the endpoint into the $( "#test12" ) value.我必须获取$( "#test12" )的值并将新创建的 object 从端点推送到$( "#test12" )值中。 you will now have all the updated data.您现在将拥有所有更新的数据。

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

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