Javascript Drop Down List will goes up before I se

2019-08-28 04:23发布

I've been using the below coding for adding items into my dropdownlist after receiving a command "ES" from a slave device:

    if (cmd == "ES") {
        var i;
        var cb;

        if (ret.substr(2, 2) == "04") cb = ObjByID('cbDeptStr');
        else cb = ObjByID('cbCntrStr');
        i = cb.length;
        while (i > 0) {
            cb.remove(1); // to remove all items which is already on the list.
            i--;
        }

        var oOption;
        var rows = ret.substr(4).split('|');
        var n;

        for (var i = 1; i < rows.length; ++i) {
            n = rows[i].substr(0, 4);
            if (n.length != 4) break;
            oOption = document.createElement("option");
            cb.options.add(oOption, i);
            oOption.innerHTML = n;
            oOption.value = n;

        }
    }

The command comes with the items that I need to add into the drop down list. The slave device will send this command "ES" to me every time i press the drop down list so that it will add the items into the list.

Problem is, every time I press the drop down list, I can see the items which had been added in the list, but after a second, the drop down list will go back up. I don't have time to select the item on the list. What's wrong?

标签: javascript
1条回答
爷的心禁止访问
2楼-- · 2019-08-28 04:44

You can programmatically select the dropdownlist with click()

$('#id').click();
查看更多
登录 后发表回答