列表中的选项可以在呈现后更改吗?

时间:2017-02-03 19:19:22

标签: w2ui

我有一个w2ui表单,其中包含一个w2ui Drop List of options。选择将根据用户选择用于显示表单的内容而有所不同。我的问题是:Drop List的内容可以在渲染后更改吗?

使用标准HTML控件,我会这样做:

$("#mySelect option[value='xyz']").remove();

$("#mySelect").append('<option value="abc">abc</option>');

这些操作可以使用w2ui Drop List完成吗?任何示例代码?

1 个答案:

答案 0 :(得分:1)

在w2ui 1.5中,您可以使用$jQueryElement.w2field()访问w2fild对象 - 然后对其进行操作。

示例:

var field = $("#my_input").w2field();
field.options.items = ["my", "new", "items"];
// optionally: pre-select first item
field.setIndex(0);
// if you do NOT use "setIndex" you need to call "refresh" yourself!
// field.refresh(); 

注意:setIndex()内部调用refresh() - 如上所述,在这种情况下,您无需自己调用刷新。

如果您想完全清除/清空您的字段,可以拨打field.reset()

编辑:在澄清它是关于表单字段后:

// Note: ``this`` refers to the w2form
// ``field[8]`` refers to a field of type "select"
  this.fields[8].options.items = ["my", "new", "items"];
  this.record = {
    field_select: 'new'
  };
  this.refresh();