简体   繁体   English

在JavaScript中同时执行两个动作

[英]Executing two actions simultaneously in JavaScript

I am having an issue with displaying a modal box containing a Select Options list with an option enabled based on the referring click from a list item. 我在显示包含“选择选项”列表的模式框时遇到问题,该模式框基于列表项中的引用点击启用了选项。 The problem is that the box opens only on the second click rather than with one. 问题在于该框仅在第二次单击时才打开,而没有一次单击。 The following is what I'm using to get the element of the referring click and enabling the disabled option (I'm not using event.target because I want the specific element and not just any list tag on the document): 以下是我用来获取点击点击元素并启用禁用选项的内容(我不使用event.target,因为我想要特定的元素,而不仅仅是文档上的任何列表标签):

function getli() {
    var clicks = document.getElementsByTagName("li");
    var clicksCount = clicks.length;
    for (var i = 0; i <= clicksCount; i += 1) {
        if (typeof clicks[i] !== 'undefined') {
            clicks[i].onclick = function(e) {
                var x = this.id;
                document.getElementById("id01").style.display="block";
                document.getElementById("type").options[x].disabled = false;
            };
        }
    }
}

The HTML: HTML:

<ul>
<li id="Choose an Option</li>
<li id="text" onclick="getli();">Text</li>
<li id="textarea" onclick="getli();">Text Area</li>
<li id="hidden" onclick="getli();">Hidden</li>
</ul>

The disabled options: 禁用的选项:

  <select name="type" id="type">
    <option>Choose</option>
    <option id="text" disabled value="text">Text</option>
    <option id="textarea" disabled value="textarea">Text Area</option>
    <option id="hidden" disabled value="hidden">Hidden</option>
    </select>

I've tried: 我试过了:

function modal() {
    document.getElementById("id01").style.display="block";
    getli();
}

and

function twobirds() {
    modal();
    getli();
}

but while both open the modal there doesn't seem to be any operation on the list as all items remain disabled. 但是虽然两者都打开了模态,但由于所有项目均保持禁用状态,因此列表上似乎没有任何操作。

Please help to set me straight. 请帮我弄直。 No jQuery please!! 请不要jQuery !!

It seems like the function you have set as onclick handler in html only sets another handler which sets the option as displayed and not-disabled. 似乎您在html中设置为onclick处理程序的函数仅设置了另一个处理程序,该处理程序将选项设置为显示且未禁用。

Sorry, for not writing code here, on phone right now. 抱歉,因为现在不在手机上在此处编写代码。 Will return here later... 稍后再返回这里...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM