在 JQuery Mobile 的选择菜单中设置默认选择?
我正在尝试在 JQuery mobile 的选择菜单中设置默认选择。文档有这个:
var myselect = $("select#foo");
myselect[0].selectedIndex = 3;
myselect.selectmenu("refresh");
我在创建菜单的正下方添加了它,但是当我运行代码时,我收到此错误:
抛出“在初始化之前无法调用“+名称+”上的方法;“+ “尝试调用方法'”+选项+“'”;
不知道此时该做什么...
I'm trying to set the default selection in a select menu in JQuery mobile. The docs have this:
var myselect = $("select#foo");
myselect[0].selectedIndex = 3;
myselect.selectmenu("refresh");
Which I added right underneath where I create the menu, but then when I run the code I get this error:
throw "cannot call methods on " + name + " prior to initialization; " +
"attempted to call method '" + options + "'";
Not sure what to do at this point...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您正在尝试使用尚未加载的 javascript 操作 DOM 对象。将 JavaScript 移至您要操作的表单之外,或者将您的代码移至在文档加载时执行的函数中。
例如:
其中
PAGE
是您正在加载的页面的 ID,该页面包含您要操作的菜单。编辑:
我根据 jinyuan 关于 JQuery Mobile 事件。
You're trying to manipulate DOM objects with javascript that haven't loaded yet. Move the javascript past the form you're trying to manipulate, or more your code into a function that executes on document load.
For example:
where
PAGE
is the id of the page you're loading that contains the menu you want to manipulate.EDIT:
I updated the example to use the JQuery Mobile pagecreate event based on jinyuan's comment regarding JQuery Mobile events.
作为 jQuery 函数:
这未经验证但有效。
As jQuery function:
This isn't validated but works.
有用。也许您可能想提供更多详细信息。
http://jsbin.com/ekayu3/2
it works. maybe you might want to provide more details.
http://jsbin.com/ekayu3/2