這是我對動態字段驗證的編碼。 如果我使用這個函數,我得到'TypeError:document.getElementById(...)爲null'這種錯誤。TypeError:document.getElementById(...)在javascript中爲null
function sr_answer_val(){
var cnt=parseInt(document.getElementById("add_field_cnt").value);
var submitAllow=true;
for(var i=1;i<cnt;i++){
if(document.getElementById("cust_field_"+i).value ==''){
alert('Answer Should be Mandatory');
submitAllow=false;
return false;
}
}
}
檢查HTML,如果真的有存在的所有元素(在執行時)要訪問。 – Sirko
http://stackoverflow.com/questions/14028959/why-does-jquery-or-a-dom-method-such-as-getelementbyid-not-find-the-element – basilikum
請提供您的HTML標記了。檢查yur頁面中是否存在元素__add_field_cnt__和__cust_field_ __ –