-2
if(typeof    (document.getElementById("courseId").value!=="undefined") || document.getElementById("courseId").value!==null)
    {
        Courseid = document.getElementById("courseId").value;
    } 
4

3 回答 3

9

以这种方式重写它:

if(document.getElementById("courseId") && document.getElementById("courseId").value)
{
    CourseId = document.getElementById("courseId").value;
} 
于 2013-06-20T07:41:30.090 回答
2

If you explicitly want to check for undefined and null you can do

if(document.getElementById('courseId') === null || 
   document.getElementById('courseId') === undefined) {
    //logic
}
于 2013-06-20T07:43:33.203 回答
0

An input's value will never be null or undefined - it will be the empty string, "".

于 2013-06-20T07:43:14.903 回答