简体   繁体   English

Javascript警报不停留在Mozilla firefox中

[英]Javascript alert not staying in Mozilla firefox

I wrote an alert which comes on change of a dropdown. 我写了一个警报,该警报是在下拉列表更改时发生的。 It comes for a particular value selected on the dropdown. 它是在下拉菜单中选择的特定值。 The alert is working fine in IE, but in Mozilla it stays for a moment and disappears. 该警报在IE中可以正常工作,但在Mozilla中,它会停留片刻并消失。

Below is the code for alert which gets called on onchange of a dropdown. 以下是警报的代码,该代码在下拉菜单onchange时被调用。

function psychAlert()
{
    var element = document.getElementById("service_help");
    for (var x = 0; x <element.options.length; x++) {
        if (element.options[x].selected) {
            selectedVal = element.options[x].value;
            break;
        }
    }

    if(selectedVal == '1514'){
        alert("Please call Helpline");
    }
}

Below is the dropdown code where the alert is getting called from a onchange. 以下是从onchange调用警报的下拉代码。

<html:select tabindex="<%= tabIndex.getNext()%>" property="serviceDesc" size="1" styleId="service_help" onchange="javascript:  psychAlert(); " ></html:option>
    html:optionsCollection property="serviceDescList" value="lookupCode" label="shortDescription"/>
<html:select>

Because your example doesn't include the actual data within your dropdown list, I cleaned it up and made an example that's much easier to follow and should clarify any issues you're having. 由于您的示例未在下拉列表中包含实际数据,因此我对其进行了整理,并制作了一个易于理解的示例,并应阐明您遇到的所有问题。 This code has been tested in IE 8, Firefox, and Chrome, and works without fail in all three. 该代码已在IE 8,Firefox和Chrome中进行了测试,并且在这三种代码中均能正常运行。

 var example = document.getElementById("dlExample"); example.addEventListener("change", function() { if (example.value === "3") { alert("Please call the help desk."); } }); 
 <select id="dlExample"> <option value="0">Hi</option> <option value="1">Bye</option> <option value="2">Yes</option> <option value="3">No</option> </select> 

So looking at this code. 所以看这段代码。 First, we simplified your very convoluted and overly done select . 首先,我们简化了您非常费力且过度完成的select Now you're no longer calling an onchange within the code, storing the value there, or requiring needless loops. 现在,您不再需要在代码中调用onchange,在其中存储值或不需要不必要的循环。 A simple select with all of your options. 一个简单的select与所有选项。

As for the javascript, we started by first adding a simple EventListener by finding the select with a getElementsByID (note that I set the ID in the select , the name wouldn't won't work). 至于javascript,我们首先通过添加带有getElementsByIDselect添加一个简单的EventListener (请注意,我在select设置了ID,该名称将不起作用)。 After that, we set the EventLIstener to be based off a change event. 之后,我们将EventLIstener设置为基于更改事件。 Anyone a change happens to that dropdown, it will check and verify that the dropdown value is whatever you wanted the alert to be called upon. 如果该下拉列表发生任何更改,它将检查并验证该下拉列表值是否是您希望调用警报的值。 Sometimes simple is better, and in this case, even more so. 有时简单会更好,在这种情况下甚至会更好。

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

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