2011-07-16 50 views
0

這是我用來驗證表單名稱字段的函數。此代碼在Chrome和IE中運行良好,但不在FireFox中運行。功能在Chrome和IE中正常工作,但不在FireFox中

當firbug檢查它給出了這樣的錯誤:

chkForm is not defined 

在此行中:提前

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

if (chkForm.name.value == ""). 

由於這是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> 

+1

你在哪裏定義和初始化'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; } 

捆綁以下行之後? – Mat

+0

是的,請提供HTML。 –

回答

0

可以檢查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; 
} 
+0

這仍然會引發錯誤。您需要檢查'if(typeof chkForm ===「undefined」)' –

1

這裏製作一個假設,因爲不能看到所有的代碼,通過我假設你依靠的事實,IE和Chrome允許通過一個全局變量訪問DOM與標識元素ID。

實際上你需要定義變量,並得到像這樣的節點的引用:

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

它正在工作:) – sohail

0

感謝名單給大家。現在此代碼工作,我加入我的代碼感謝:-) 蘇海爾·艾哈邁德

+0

但它沒有回到警報後的用戶名字段 chkForm.name.focus(); – sohail

相關問題