Javascript 选择菜单('刷新',真)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10551996/
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
selectmenu ('refresh', true)
提问by Dar Lym
I get form from zend framework site and put it in response in new file in function written by jquery mobile, but I get this error:
我从 zend 框架站点获取表单并将其放入 jquery mobile 编写的函数中的新文件中作为响应,但出现此错误:
uncaught exception: cannot call methods on selectmenu prior to initialization; attempted to call method 'refresh' .
未捕获的异常:无法在初始化之前调用 selectmenu 上的方法;试图调用方法 'refresh' 。
Code of function this file:
此文件的功能代码:
function addItem(id) {
$.ajax({
url:'http://zf.darina.php.nixsolutions.com/order/index/create-order-mobile',
dataType:"jsonp",
data:{id_good:id},
success:function (resp) {
console.log(resp);
$('.product-table').empty();
$('.product-table').append(resp.prod);
$('.product-table').append(resp.form);
$('.add-order-btn').button();
$('.mob-size').selectmenu('refresh', true);
$('#block').page();
}
})
}
回答by HoffZ
Force initialize the selectmenu(s) first:
首先强制初始化选择菜单:
$('.mob-size').selectmenu(); // Initializes
$('.mob-size').selectmenu('refresh', true);
or use this for short
或简称为
$('.mob-size').selectmenu().selectmenu('refresh', true);
回答by Simona Adriani
In my case, if I was not initializing the select before invoking the 'disable' method I got the error, while if I was initializing it, the select didn't disable but duplicate itself - I tried to select the object by TAG NAME instead of by CLASS or ID NAME,
就我而言,如果我在调用“禁用”方法之前没有初始化选择,则会出现错误,而如果我正在初始化它,则选择不会禁用而是复制自身 - 我尝试通过 TAG NAME 选择对象的 CLASS 或 ID NAME,
$('select').selectmenu('disable');
instead of
代替
$('.select-class-name').selectmenu('disable');
and it worked without forced initialization
它在没有强制初始化的情况下工作
回答by comeGetSome
you do this in your custom refresh delegation function:
您在自定义刷新委托函数中执行此操作:
var w = $("#yourinput");
if( w.data("mobile-selectmenu") === undefined) {
// not initialized yet, lets do so
w.selectmenu();
}
w.selectmenu("refresh",true);
according to enhancement resolution here
根据这里的增强分辨率
回答by Michelle
I found the same problem, but a more involved solution. When jqm wraps the select
element, it puts it in a <span>
element with the same class list as the select
element. I changed my reference to it so that instead of reading:
我发现了同样的问题,但更复杂的解决方案。当 jqm 包装select
元素时,它会将其放入<span>
与该元素具有相同类列表的select
元素中。我改变了对它的引用,而不是阅读:
row.find(".selectCompletion").selectmenu("disable");
it now reads:
现在写着:
row.find("select.selectCompletion").selectmenu("disable");
Specifying that jquery should only find the select
element matching the class name, rather than all elements in .row that match the class name, solved the problem.
指定jquery应该只找到select
匹配类名的元素,而不是.row中匹配类名的所有元素,解决了问题。
回答by ishahak
This happened to me when cloningexisting select element in order to duplicatethe original section multiple times.
我在克隆现有的 select 元素以多次复制原始部分时发生了这种情况。
Calling 'refresh' for the original element, worked fine, while calling it for the cloned sections was leading to the error appearing in the question.
为原始元素调用“ refresh”,效果很好,而为克隆部分调用它会导致问题中出现错误。
However, calling selectmenu()was causing a 'vandalisation' to the form, as can be seen in the following image:
但是,调用selectmenu()会导致表单“破坏”,如下图所示:
Explanation: top = original. bottom = vandalised cloned element right after calling selectmenu.
解释:顶部 = 原始。底部 = 在调用 selectmenu 后立即破坏克隆元素。
Solution:
解决方案:
The following code solved this vandalisation problem:
以下代码解决了这个破坏问题:
cloned_elem.find('select[name=MyClass]').selectmenu().selectmenu("destroy").selectmenu();
cloned_elem.find('select[name=MyClass]') .selectmenu().selectmenu("destroy").selectmenu();
This is not an ideal solution because we must call the first selectmenu() in order to call selectmenu("destroy"), so I would be glad to hear of a cleaner solution.
这不是一个理想的解决方案,因为我们必须调用第一个 selectmenu() 才能调用 selectmenu("destroy"),所以我很高兴听到更简洁的解决方案。