简体   繁体   中英

How to change select box value using JCF plugin?

I have a select box that uses jcf plugin. As documenation says we have to refresh method to change the value but it is not changing. Below here is what i have tried?

 $(function() { jcf.replaceAll(); }); function changeValue() { $("#mgloc_npgpo").val('1') jcf.getInstance($("#mgloc_npgpo")).refresh() }
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/jcf/1.2.3/js/jcf.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/jcf/1.2.3/js/jcf.select.js"></script> <div class="select-holder"><label>Selection</label> <div class="select-wrap"><input type="hidden" id="mgloc_340b" value=""/> <div class="select-wrap"><input type="hidden" id="mgloc_340b" value=""/> <select id="mgloc_npgpo" multiple="multiple" data-jcf="{&quot;wrapNative&quot;: false, &quot;wrapNativeOnMobile&quot;: false, &quot;useCustomScroll&quot;: false, &quot;multipleCompactStyle&quot;: true}" class="jcf-hidden"> <option value="" selected="selected">Select Non-Primary</option> <option value="1">value 1</option> <option value="2">value 2</option> </select> <div class="errorMsg hide">&#160;</div> </div> <div class="errorMsg hide">&#160;</div> </div> <button style="margin-top: 100px;" onClick="changeValue()">change value</button> </div>

You have a typo

Your id name mgloc_npgpo not mgloc_ngpo

In your js code you have used mgloc_ngpo at every place. which should be mgloc_npgpo

 $(function() { jcf.replaceAll(); }); function changeValue() { $("#mgloc_npgpo").val('1') jcf.getInstance($("#mgloc_npgpo")).refresh() }
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/jcf/1.2.3/js/jcf.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/jcf/1.2.3/js/jcf.select.js"></script> <div class="select-holder"><label>Selection</label> <div class="select-wrap"><input type="hidden" id="mgloc_340b" value=""/> <select jcf id="mgloc_npgpo" data-jcf="{&quot;wrapNative&quot;: false, &quot;wrapNativeOnMobile&quot;: false, &quot;useCustomScroll&quot;: false, &quot;multipleCompactStyle&quot;: true}" class="jcf-hidden"> <option value="" selected="selected">Select Non-Primary</option> <option value="1" >value1</option> <option value="2" >value2</option> </select> <div class="errorMsg hide">&#160;</div> </div> <button onClick="changeValue()">change value</button> </div>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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