Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/435.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 功能在Chrome和IE中运行良好,但在FireFox中运行不正常_Javascript_Forms_Validation - Fatal编程技术网

Javascript 功能在Chrome和IE中运行良好,但在FireFox中运行不正常

Javascript 功能在Chrome和IE中运行良好,但在FireFox中运行不正常,javascript,forms,validation,Javascript,Forms,Validation,这是我用来验证表单名称字段的函数。这段代码在Chrome和IE中运行良好,但在FireFox中却不行 使用firbug进行检查时,会出现以下错误: chkForm is not defined 在这一行: if ( chkForm.name.value == "" ). 提前谢谢 function uname() { if ( chkForm.name.value == "" ) { alert("Please fill in Username box"

这是我用来验证表单名称字段的函数。这段代码在Chrome和IE中运行良好,但在FireFox中却不行

使用firbug进行检查时,会出现以下错误:

chkForm is not defined
在这一行:

if ( chkForm.name.value == "" ).
提前谢谢

function uname()
{   
    if ( chkForm.name.value == "" )
    {
        alert("Please fill in Username box");
        chkForm.name.focus();
        return false;
    }
    return true;
}
这是html表单

<form name="chkForm" id="chkForm" method="post" action="" onsubmit="return Form_Validator(this)">
<table border="0" cellpadding="0" cellspacing="0" width="550" id="table1">
    <tr>
        <td width="135">&nbsp;</td>
        <td width="138">&nbsp;</td>
        <td width="215">&nbsp;</td>
    </tr>
    <tr>
        <td width="135">Username</td>
        <td width="138">
        <input type="text" name="name" id="username" onblur="return uname()" size="20" class="input_s1_normal"></td>
        <td width="215">
        <div id="nameInfo" align="left"></div>
        </td>
    </tr>
    <tr>
        <td width="135">Email</td>
        <td width="138">
        <input type="text" name="email" id="email" size="20" class="input_s1_normal"></td>
        <td width="215">
        <div id="emailInfo" align="left"></div>
        </td>
    </tr>       
    <tr>
        <td width="135">&nbsp;</td>
        <td width="138">
        <input type="submit" value="SAVE" name="B1" class="button_s1"></td>
        <td width="215">&nbsp;</td>
    </tr>
</table>

用户名
电子邮件

您可以检查chkForm是否是这样定义的

这可能不是一个理想的方法,但你会得到如何做到这一点的一般想法

function uname()
{   
    if(!(chkForm)){
        chkForm = document.getElementByID('chkForm');
    }
    if ( chkForm.name.value == "" )
    {
        alert("Please fill in Username box");
        chkForm.name.focus();
        return false;
    }
    return true;
}

这里做一个假设,因为你看不到你所有的代码,我假设你依赖的是IE和Chrome允许通过ID的全局变量访问ID元素的dom

您需要实际定义变量并获得对节点的引用,如下所示:

var chkForm = document.getElementById('chkForm');

谢谢大家。在我的代码中添加以下行后,此代码现在可以工作了

var chkForm = document.getElementById('chkForm');


function uname()
{   
    var chkForm = document.getElementById('chkForm');
    if ( chkForm.name.value == "" )
    {
        alert("Please fill in Username box");
        chkForm.name.focus();
        return false;
    }
     return true;
}
万分感谢:-)
Sohail Ahmad

您在哪里定义和初始化
chkForm
?是的,请同时提供HTML。这仍然会引发错误。您需要检查
是否(typeof chkForm==“undefined”)
但在发出警报chkForm.name.focus()后,它没有聚焦回用户名字段;