-3
function test()
{
    var testcodevalue = document.getElementById("testcode").value;
    var testcodetext = document.getElementById("testcode").text;    
    if(testcodetext == undefined)
    {
       formObject.testcode.value = "";
       formObject.testcode.text = "";
    }
}

在上面的脚本中,onload我得到testcodevalueas object 和testccodetextas的值undefined。我testcode.value正在设置到一个文本框中。问题是每次onload我得到testcodetextasundefined并且在文本框中设置了相同的值。我该如何清除这个?

4

1 回答 1

0

使用 typeof 检查它是否未定义:

function test()
        {

                var testcodevalue = document.getElementById("testcode").value;
                var testcodetext = document.getElementById("testcode").text;    
                if(typeof testcodetext === "undefined")
                {
                   formObject.testcode.value =  "";
                   formObject.testcode.text =   "";
                 }
        }
于 2013-10-30T18:18:13.230 回答