选择默认情况下使用jQuery选择一个选项 [英] Chosen select an option by default with jQuery

查看:164
本文介绍了选择默认情况下使用jQuery选择一个选项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带选项的选择框,我在Zend View上使用jQueryChosen。这个插件工作正常。

  Javascript:
$('#select_type')。

我的问题是我需要在页面加载时选择其中一个选项,具体取决于值type_id是。我试过这个:

  document.getElementById('select_type')。value ='<?= $ this-> type_id ?>'; 

这改变了它的值,但是在选择的JQuery中没有选择该选项。



有什么想法?




编辑:但它不更新/更改jQuery选择插件中的选定元素。我的问题是关于更新jQuery的选定值选择不是HTML SELECT(我已经成功完成)。 这里是jQuery Chosen插件的链接

解决方案

使用JS更新列表



运行后: document.getElementById('select_type')。value ='<?= $ this-> type_id?>';



对于选择的版本1及以上版本(更新版本)



您应该打电话:

  $('#select_type')。trigger('chosen:updated'); 

取自选择文档


更新动态选择

如果您需要要更新选择字段中的选项并希望选择选取更改,您需要在该字段上触发选择:更新事件。 Chosen将根据更新后的内容重新构建自己。


$ b $ p $(#form_field)。trigger(chosen:updated);


请参阅更改日志,以便公布此API更改。



对于低于1(较旧)的选定版本



您应该打电话:

  $('#select_type')。trigger('liszt :更新'); 

取自选择文档


更新动态选择

如果您需要更新您的选项选择字段并希望选择
来获取更改,您需要触发该字段上的liszt:updated
事件。 Chosen会根据更新的
内容重新构建自己。


$ b

  • jQuery版本 $(#form_field)。trigger(liszt:updated);

  • 原型版本 Event.fire($(form_field),liszt:updated);




设置调用Chosen之前的值



为什么不在JS中做这个选择?用JavaScript调用Chosen插件之前的HTML?你正在设定一块PHP的价值,所以我不明白为什么不能这样做。


I have one select box with options and I am using jQuery "Chosen" on a Zend View. This plugin is working fine.

Javascript:
$('#select_type').chosen();

My problem is I need to select one of the options when the page loads depending on what the value of type_id is. I tried this:

document.getElementById('select_type').value = '<?=$this->type_id?>';

This changes its value but the option is not selected in the JQuery chosen.

Any ideas?


EDIT: The option successfully changes by doing what I have mentioned above but it does not update/change the selected element in jQuery "chosen" plugin. My question is about updating the selected value for jQuery Chosen not the HTML SELECT (which I have done successfully). Here is a link to jQuery Chosen plugin

解决方案

Update list with JS

After you run:

document.getElementById('select_type').value = '<?=$this->type_id?>';

For Chosen version 1 and above (newer)

You should call:

$('#select_type').trigger('chosen:updated');

Taken from the Chosen documentation:

Updating Chosen Dynamically

If you need to update the options in your select field and want Chosen to pick up the changes, you'll need to trigger the "chosen:updated" event on the field. Chosen will re-build itself based on the updated content.

$("#form_field").trigger("chosen:updated");

See the change log for the announcement of this API change.

For Chosen versions below 1 (older)

You should call:

$('#select_type').trigger('liszt:updated');

Taken from the Chosen documentation:

Updating Chosen Dynamically

If you need to update the options in your select field and want Chosen to pick up the changes, you'll need to trigger the "liszt:updated" event on the field. Chosen will re-build itself based on the updated content.

  • jQuery Version: $("#form_field").trigger("liszt:updated");
  • Prototype Version: Event.fire($("form_field"), "liszt:updated");

Set the value before calling Chosen

Instead of doing this from JS why don't you just set the selected option in the HTML before calling the Chosen plugin with JavaScript? You are setting the value from a piece of PHP so I don't see why this couldn't be done.

这篇关于选择默认情况下使用jQuery选择一个选项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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