【问题标题】:HTML DOM: How to call a SELECT box's update method?HTML DOM:如何调用 SELECT 框的更新方法?
【发布时间】:2009-09-12 08:59:59
【问题描述】:

我正在编写一个与 HTML DOM 交互的 VB 应用程序。

如果我去这个页面:

https://edit.yahoo.com/registration

然后尝试将右上角的“我更喜欢内容来自”选择框从选择的默认选项更改为“雅虎!亚洲”,我可以使用以下命令:

Document.Forms.regFormBody.preferredcontent.selectedIndex = 10

它成功地改变了它。但问题是它与手动将选项更改为 Yahoo! 的行为不同。亚洲使用您的鼠标。

如果您尝试在不使用 DOM 的情况下手动执行此操作,则在更改它后,页面会刷新。

我假设它是一个 javascript 调用或根据选择的选项刷新该页面的东西,但我尝试使用 DOM 复制该行为,但我无法弄清楚。

有人可以帮忙吗?提前致谢。

【问题讨论】:

    标签: html dom html-select


    【解决方案1】:

    您正在寻找onChange 事件。当然,表单有可能被其他事件提交,可能是onBlur

    更新了来自 cmets 的其他问题

    显然受以下行约束:

    YAHOO.util.Event.on("preferredcontent", "change", ymem_reg.switchPrefContent);
    

    调用如下:

    ymem_reg.switchPrefContent = function(e) {
        var Dom = YAHOO.util.Dom || false;
        if (Dom) {
            var oForm = Dom.get("regFormBody");
            var sToIntl = this.options[this.selectedIndex].value;
            var yregFormValidator = (typeof yregFormValidator !== "undefined") ? yregFormValidator: false;
            var fieldsWithDefaults = Dom.get(["firstname", "secondname", "dd", "yyyy"]);
            var clearVal = function(el) {
                if (el !== null && ymem_reg.fieldHasDefaultVal(el)) {
                    el.value = "";
                }
            }
            if (oForm !== null && sToIntl !== "") {
                if (yregFormValidator) {
                    yregFormValidator.disable();
                }
                var oInput = document.createElement("input");
                oInput.setAttribute("type", "hidden");
                oInput.setAttribute("name", "IntlSwapBtn");
                oInput.setAttribute("id", "IntlSwapBtn");
                oForm.appendChild(oInput);
                Dom.batch(fieldsWithDefaults, clearVal);
                oForm.submit();
            }
        }
    }
    

    【讨论】:

    • 但是如果您检查有问题的 SELECT 框,您会注意到显然没有绑定到它的 onChange 行为。你能找到它改变时的 javascript 调用吗?
    • 太棒了。不知道你是如何设法剖析的,但我会接受的。但我仍然无法弄清楚如何使用 DOM 来复制该行为。我尝试在 VB 中执行此操作:' Document.Forms.regFormBody.preferredcontent.selectedIndex = 10 ' Set newElem = IE.Document.createElement("button") ' newElem.setAttribute "onclick", "ymem_reg.switchPrefContent();" ' IE.Document.Body.InsertBefore newElem, IE.Document.Body.FirstChild ' IE.Document.Body.FirstChild.Click 但这不起作用:( 新按钮已创建,但当我单击它时,什么也没有发生。还有更多提示吗?
    猜你喜欢
    • 2019-03-08
    • 1970-01-01
    • 2017-09-30
    • 2021-12-27
    • 2018-03-18
    • 2018-11-24
    • 1970-01-01
    • 2021-08-30
    • 2021-06-03
    相关资源
    最近更新 更多