简体   繁体   English

Javascript indexOf条件表现不符合预期

[英]Javascript indexOf condition not behaving as expected

I have a javascript function that runs on window.onload: 我有一个在window.onload上运行的javascript函数:

if(window.onload) {
        var curronload = window.onload;
        var newonload = function() {
            curronload();
            formatICCID_IMEI();
        };
        window.onload = newonload;
    } else {
        window.onload = formatICCID_IMEI;

function formatICCID_IMEI() {
        var IMEI = $find("<%=cbIMEI.ClientID %>");
        alert(IMEI.get_textBoxControl().value);
        alert(IMEI.get_textBoxControl().value.indexOf("."));
        if (IMEI.get_textBoxControl().value.indexOf(".") > -1) {
            alert("Hi!");                
        }

    }

I'm using this more elaborate way of calling my function from this link because if I just use window.onload or document.onload, my control (cbIMEI) is not found. 我正在使用通过此链接调用函数的更精细的方法,因为如果仅使用window.onload或document.onload,则找不到控件(cbIMEI)。 Using this more elaborate method, I don't have that problem. 使用这种更精细的方法,我没有这个问题。 However, my function formatICCID_IMEI is acting strangely. 但是,我的函数formatICCID_IMEI行为异常。 I don't know if it's due to the way I'm calling formatICCID_IMEI, or just something in formatICCID_IMEI that I'm not seeing. 我不知道这是由于我调用formatICCID_IMEI的方式还是由于我没有看到的formatICCID_IMEI引起的。 If I comment out 如果我注释掉

if (IMEI.get_textBoxControl().value.indexOf(".") > -1) {
            alert("Hi!");  

the first and second alerts tell me that 第一和第二警报告诉我

IMEI.get_textBoxControl().value = 351937.04.230880.7

and that 然后

IMEI.get_textBoxControl().value.indexOf = 6

all as expected. 一切都如预期。 HOWEVER, if I comment out the two above alert lines and uncomment the IF condition, the line 但是,如果我注释掉上面的两个警报行并取消注释IF条件,则该行

alert("Hi!");

never runs. 永远不会运行。 If I uncomment all lines, none of the alerts run. 如果我取消注释所有行,则不会运行任何警报。 The same behavior holds true if I'm in debug mode. 如果我处于调试模式,则相同的行为也适用。 If the condition is uncommented, my cursor never gets to my function at all. 如果条件未注释,则我的光标根本不会进入函数。 What the heck? 有没有搞错?

You have no close bracket for the if(window.onload) condition - is that intentional? 您没有if(window.onload)条件的右括号-这是故意的吗?

Since you're using jQuery, why are you not just using the standard $(document).ready stuff? 既然您使用的是jQuery,为什么不只使用标准的$(document).ready东西呢?

function formatICCID_IMEI() {
    var IMEI = $find("<%=cbIMEI.ClientID %>");
    alert(IMEI.get_textBoxControl().value);
    alert(IMEI.get_textBoxControl().value.indexOf("."));
    if (IMEI.get_textBoxControl().value.indexOf(".") > -1) {
        alert("Hi!");                
    }

}

$(document).ready(formatICCID_IMEI);

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

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