FilteringSelect第二次不工作

时间:2013-08-22 09:47:17

标签: javascript dojo datastore

我在测试文件中有以下内容。我想要的是将选择的值从filteringselect写入divid。它第一次正常工作,但下次没有进行过滤(下拉列表中没有值)。 对于filteringselect的第一次更改,onchange被成功调用。

请帮助我找到问题所在。我是道场新手。 DOJO版本 - DOJO 1.8

<script>
    require([
        "dojo/store/Memory", "dijit/form/FilteringSelect", "dojo/domReady!"
    ], function(Memory, FilteringSelect){
        var stateStore = new Memory({
            data: [
                {name:"Alabama", id:"AL"},
                {name:"Alaska", id:"AK"},
                {name:"American Samoa", id:"AS"},
                {name:"Arizona", id:"AZ"},
                {name:"Arkansas", id:"AR"},
                {name:"Armed Forces Europe", id:"AE"},
                {name:"Armed Forces Pacific", id:"AP"},
                {name:"Armed Forces the Americas", id:"AA"},
                {name:"California", id:"CA"},
                {name:"Colorado", id:"CO"},
                {name:"Connecticut", id:"CT"},
                {name:"Delaware", id:"DE"}
            ]
        });

        var filteringSelect = new FilteringSelect({
            id: "stateSelect",
            name: "state",
            placeHolder: "Select a State",
            store: stateStore,
            searchAttr: "name",
            onChange: function(value) {
                var text1 = document.getElementById("divid").innerHTML
                document.getElementById("divid").innerHTML = text1 + dijit.byId('stateSelect').attr('displayedValue') + ';';
            }
        }, "stateSelect");
    });
</script>

<div id="divid">
    Select a State : <input id="stateSelect">
</div>

1 个答案:

答案 0 :(得分:1)

您的错误是由于您记录输出的方式:

var text1 = document.getElementById("divid").innerHTML;
document.getElementById("divid").innerHTML = text1 + [...]

由于'divid'包含FilteringSelect dijit标记,再次设置divid.innerHTML会覆盖现有的DOM并混淆现有的FilteringSelect dijit实例。通常,您不应该修改dijit实例的DOM。 只需在标记中添加一个空的span元素即可记录您的值:

<div id="divid">
Select a State : <input id="stateSelect">
<span id="buffer"></span>
</div>

和onChange成为:

onChange: function(value){
var text1 = document.getElementById("buffer").innerHTML;
document.getElementById("buffer").innerHTML = text1 + [...]
}