使用jquery.selectbox时无法设置SELECT控件的值



在使用jquery.selectbox.js插件制作好看的SELECT下拉列表时,我遇到了一个问题,无法使用jquery选择当前值:$("#SELECT_id").val("my value");

可能的解决方案是删除$('#select_id').selectbox();电话,但我失去了设计。

该怎么办?

调查:

当使用jquery.selectbox时,真正的下拉列表隐藏在页面上,只有jquery.seelectbox创建的表示元素可见(实际上它们是DIV)。问题是,当.val()更新时,相应的可见DIV(带有class="jquery selectbox currentItem")不会更新。

不是最佳解决方案,但比你提出的解决方案更干净,试试这个(基本上,强制selectBox更新其html):

$("#order_billing_region").val("your value").selectBox("options", $("#order_billing_region").html());

我找到了一个解决方法,我们可以分离selectbox,然后设置值,并将selectbox插件再次附加到下拉列表中。

function fnClearJquerySelectBox(control,value)
{
   $("#order_billing_region").selectbox("detach");
   $("#order_billing_region").val(value);
   $("#order_billing_region").selectbox("attach");
}
fnClearJquerySelectBox("control","0");

希望这能有所帮助。。

我发现以下内容:https://github.com/marcj/jquery-selectBox/blob/master/readme.md

代码:$('select').selectBox('value',1);

我发明了一种变通方法:

var ctlName = "#order_billing_region";
var ctl = $(ctlName);
ctl.val("my value");
//A workaround for buggy jquery.selectbox
ctl.siblings(".jquery-selectbox-currentItem").html($(ctlName + " option:selected").html());

最新更新