我有一个带有选项的选择框,我使用的是Zend上的jQuery“select”。这个插件很好用。
Javascript:
$('#select_type').chosen();
我的问题是,在页面加载时,我需要选择一个选项,这取决于type_id的值。我试过这个:
document.getElementById('select_type').value = '<?=$this->type_id?>';
这会改变它的值,但是在所选的JQuery中没有选择该选项。
有什么想法吗?
编辑:该选项通过执行我前面提到的操作成功地改变了,但它不更新/更改jQuery中的选定元素“选择”插件。我的问题是更新选定的jQuery值,而不是HTML (这是我成功完成的)。Here is a link to jQuery Chosen plugin
发布于 2012-02-06 13:53:27
用JS更新列表
你跑完之后:
document.getElementById('select_type').value = '<?=$this->type_id?>';
用于选定的版本1及以上(更新版本)
你应该打电话给:
$('#select_type').trigger('chosen:updated');
摘自Chosen documentation
如果需要更新select字段中的选项,并希望选择以获取更改,则需要在字段中触发“
:updated”事件。选择将根据更新的内容重新构建自己。
$("#form_field").trigger("chosen:updated");
有关此API更改的公告,请参见the change log。
所选版本低于1(旧版本)
你应该打电话给:
$('#select_type').trigger('liszt:updated');
摘自Chosen documentation
如果需要更新select字段中的选项并希望选择这些更改,则需要在字段上触发“liszt:
更新”事件。选择将根据更新的内容重新构建自己。
$("#form_field").trigger("liszt:updated");
Event.fire($("form_field"), "liszt:updated");
在调用选定的值之前设置值
与其从JS中这样做,不如在用JavaScript调用所选的插件之前,先在HTML中设置选中的选项?您正在从PHP中设置值,所以我不明白为什么不能这样做。
发布于 2012-11-26 00:04:03
您只需使用选中的属性设置正确的元素,如下所示:
<select name="teams[]" data-placeholder="Chose an option:"
class="chzn-select" multiple tabindex="6">
<option value=""></option>
<option selected="selected">Dallas Cowboys</option>
<option selected="selected">New York Giants</option>
<option>Philadelphia Eagles</option>
<option>Washington Redskins</option>
</select>
selected将小心初始化自身并显示两个预选的选项。
发布于 2013-09-02 09:59:52
试试这个:
$("#select_type").val(<?=$this->type_id?>).trigger('change');
$('#select_type').trigger('liszt:updated');
https://stackoverflow.com/questions/9160988
复制相似问题