通过javascript将jQuery UI selectmenu设置为特定选项 [英] Set a jQuery UI selectmenu to a specific option by javascript

查看:87
本文介绍了通过javascript将jQuery UI selectmenu设置为特定选项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个类似于以下内容的选择菜单

I have a selectmenu like the following

<select name="scale" id="scale">
  <option selected>linear</option>
  <option>root</option>
  <option>square</option>
</select>

为了变得更好,我使用jQuery UI v1.11.2.

To make it nice I use jQuery UI v1.11.2.

$('#scale').selectmenu();

我现在可以读取下拉菜单的值

I can now read the value of the dropdown by

alert($('#scale').val());

得出线性"的答案.我也可以通过使用

which results in 'linear' as the answer. I can also set the value to 'square' by using

$('#scale').val('square');
alert($('#scale').val());

正确给出答案平方".但是(!!!)下拉列表不会在屏幕上显示.因此,实际上我可以设置和读取该值,但视觉表示不会改变-小部件不会刷新.我读到某个地方扔了.change(),但没有任何效果.我还尝试了类似

which correctly gives the answer 'square'. But (!!!) the dropdown does not display this on the screen. So actually I can set and read the value, but the visual representation doesn't change - the widget does not refresh. I read somewhere to throw a .change() but without any effect. I also tried answers like in jQuery UI Selectmenu value set dynamically does not change visible selected value but failed. Any $('#scale').selectmenu('value', 'square'); resuts in the error message Error: no such method 'value' for selectmenu widget instance.

在将小部件设置为新值后,谁能帮助您刷新该小部件?

Can anyone help how to refresh the widget after setting it to a new value?

推荐答案

如果您仅调用

It should work as you expect it to if you just call refresh after using .val():

$('#scale').val('square');
$("#scale").selectmenu("refresh");

这样做的原因是.val()函数由jQuery提供,而小部件是jQueryUI的一部分. [大概$ .ui作者不想更改$功能,尽管这纯粹是我的推测]

The reason for this is that the .val() function is provided by jQuery, while the widget is part of jQueryUI. [presumably the $.ui authors didn't want to alter the $ functionality, though this is purely speculation on my part]

这篇关于通过javascript将jQuery UI selectmenu设置为特定选项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆