jQuery UI Selectmenu 值动态设置不会改变可见的选定值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9917516/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
jQuery UI Selectmenu value set dynamically does not change visible selected value
提问by SocioBit
I am using jQuery UI Selectmenu and am having trouble setting the value of a rendered selected. It seems to change the selected option in the underlying select, but the selectmenu does not show the change. I am calling .selectmenu('refresh', true) but nothing happens.
我正在使用 jQuery UI Selectmenu 并且无法设置呈现的选定值。似乎更改了底层选择中的选定选项,但选择菜单未显示更改。我正在调用 .selectmenu('refresh', true) 但没有任何反应。
Here is an example: http://jsfiddle.net/sociobit/wYBeL/
这是一个例子:http: //jsfiddle.net/sociobit/wYBeL/
回答by Tats_innit
Hiya So demo(Solution) http://jsfiddle.net/wYBeL/43/instead of refresh try .selectmenu("value", selectedValue);
OR (Hack) http://jsfiddle.net/wYBeL/36/(keeps the selectpopup setting of yours :) What ever suits you
Hiya So演示(解决方案)http://jsfiddle.net/wYBeL/43/而不是刷新尝试.selectmenu("value", selectedValue);
或(Hack)http://jsfiddle.net/wYBeL/36/(保留您的 selectpopup 设置:)什么都适合你
So I checked the DOM using firebug and it seems the selectmenu() style: popup adds extra layer of styling but the #sel2 value is set correctly and you just need to setup the right element with correct value. I reckon refresh will work when you will have an ajax populating a dropdown and refreshing part of page.
所以我使用 firebug 检查了 DOM,似乎 selectmenu() style: popup 添加了额外的样式层,但 #sel2 值设置正确,您只需要使用正确的值设置正确的元素。我认为刷新将起作用,当您将使用 ajax 填充下拉列表并刷新页面的一部分时。
Hmm, you can try looking at API for more details and In case if you don't need selectmenu popup then without it this will work as well like @jiimw said: (BUt styling goes weird) : http://jsfiddle.net/wYBeL/35/;Please let me know if this doesn't help I will remove post.
嗯,您可以尝试查看 API 以获取更多详细信息,如果您不需要 selectmenu 弹出窗口,那么如果没有它,这将像 @jiimw 所说的那样工作:(但样式很奇怪):http: //jsfiddle.net/ wYBeL/35/;如果这没有帮助,请告诉我,我将删除帖子。
extra link: http://wiki.jqueryui.com/w/page/12138056/Selectmenu
额外链接:http: //wiki.jqueryui.com/w/page/12138056/Selectmenu
Hope both helps.
希望两者都有帮助。
JQuery Code here
JQuery 代码在这里
$(function() {
$('select').selectmenu({
style: 'popup'
});
$('#chksame').click(function() {
if ($(this).is(':checked')) {
var selectedValue = $('#sel1').val();
$('#sel2').val(selectedValue);
$('#sel2').selectmenu("value", selectedValue);
}
});
});?
** OR **
** 或者 **
$(function() {
$('select').selectmenu({
style: 'popup'
});
$('#chksame').click(function() {
if ($(this).is(':checked')) {
var selectedValue = $('#sel1').val();
$('#sel2').val(selectedValue);
// set the right element with the select value
$('#sel2-button span').text($("#sel2 option[value='" + selectedValue +"']").text());
$('#sel2').selectmenu('refresh', true);
}
});
});?
Cheers
干杯