[英]Change text for `option` in a `select` with jquery
So, i´m trying to change a text for an option with jquery. 因此,我正在尝试使用jquery更改选项的文本。
HTML 的HTML
<select id="mySelect">
<option value="change me">Change me</option>
</select>
JS JS
$(document).ready(function() {
$('#mySelect').select2();
$(document).find('option[value="change me"]').text('Changed');
})
Well, nothing happends. 好吧,什么都没发生。
It´s important to change the text "live". 更改文本“实时”很重要。
Fiddle: https://jsfiddle.net/gmt22ffL/2/ 小提琴: https : //jsfiddle.net/gmt22ffL/2/
Is this even possible? 这有可能吗?
I´m using Jquery plugin "Select2" for my select. 我正在使用Jquery插件“ Select2”进行选择。
It works perfectly fine . 它工作得很好 。 But let's try adding Select2 and see:
但是,让我们尝试添加Select2并查看:
$(document).ready(function() { $('option[value="change me"]').text('Changed'); $('#mySelect').select2(); });
<script src="https://code.jquery.com/jquery-1.11.3.js"></script> <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.1/css/select2.min.css" /> <script src="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.1/js/select2.full.min.js"></script> <select id="mySelect"> <option value="change me">Change me</option> </select>
Swapping the lines work. 交换线路工作。
Destroying and changing: 销毁和更改:
$(document).ready(function() { $('#mySelect').select2(); $('#mySelect').select2("destroy"); $('option[value="change me"]').text('Changed'); $('#mySelect').select2(); });
<script src="https://code.jquery.com/jquery-1.11.3.js"></script> <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.1/css/select2.min.css" /> <script src="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.1/js/select2.full.min.js"></script> <select id="mySelect"> <option value="change me">Change me</option> </select>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.