getelementbyid在firefox中不起作用

时间:2010-04-29 18:51:03

标签: javascript

下面提到的代码在Internet Explorer中工作得很完美,但在firefox中却没有...我在firefox中遇到错误:document.getElementById(“supplier_no”)。value = values_array [0]; getElementById返回null。如何解决问题?


var winName;      //variable for the popup window
var g_return_destination = null ; //variable to track where the data gets sent back to.


// Set the value in the original pages text box.
function f_set_home_value( as_Value )
{
 if (document.getElementById(g_return_destination[0]).name == "netbank_supplier_name_info" )
 {
  //clear the old values
  for (selnum = 1; selnum <= 5; selnum++)
  {
   document.getElementById("expense_account"+selnum).value = "";
   document.getElementById("expense_account_name"+selnum).value = "";
   document.getElementById("expense_vat_flag"+selnum).value = "off";
   document.getElementById("expense_vat_flag"+selnum).checked = "";
   document.getElementById("expense_vat_amount"+selnum).value = "";
   document.getElementById("expense_vat_code"+selnum).value = "";
   document.getElementById("expense_period"+selnum).value = "";
   document.getElementById("expense_date"+selnum).value = "";
   if (selnum!=1) {//these are sometimes defaulted in, and in any case you will always have line1
    document.getElementById("expense_more_dept"+selnum).value = "";
    document.getElementById("expense_more_prj"+selnum).value = "";
    document.getElementById("expense_more_subj"+selnum).value = "";
   }
   document.getElementById("expense_amount"+selnum).value = "";
  }
  var values_array = as_Value[0].split("!");
  document.getElementById("supplier_no").value= values_array[0];
  document.getElementById("supplier_bankAccount_no").value= values_array[1];
  str = values_array[2] ;
  str = str.split(";sp;").join(" ");
  document.getElementById("default_expense_account").value= str;
  document.getElementById("expense_account1").value= str; 
  document.getElementById("expense_more_sok1").disabled= false; 
  str = values_array[3] ;
  str = str.split(";sp;").join(" ");
  document.getElementById("payment_term").value= str;
  strPeriod = calcPeriod(str,document.getElementById("due_date").value);
  document.getElementById("expense_period1").value = (strPeriod); 
  strExpenseDate = calcExpenseDate(str,document.getElementById("due_date").value);
  document.getElementById("expense_date1").value = (strExpenseDate); 
  str = values_array[4] ;
  str = str.split(";sp;").join(" ");
  document.getElementById("expense_account_name1").value= str;
  str = values_array[5] ;
  str = str.split(";sp;").join(" ");
  document.getElementById("expense_vat_code1").value= str;
  if (str == 0) { 
   document.getElementById("expense_vat_flag1").checked= '';
   document.getElementById("expense_vat_flag1").disabled= true;
  }else{
   document.getElementById("expense_vat_flag1").checked= 'yes';
   document.getElementById("expense_vat_flag1").value= 'on';
   document.getElementById("expense_vat_flag1").disabled= false;
  }
  str = values_array[6] ;
  str = str.split(";sp;").join(" ");
  document.getElementById("supplier_name").value= str;
  var str  = values_array[7];
  str = str.split(";sp;").join(" ");
  str = str.split("&cr;").join("\r");
  document.getElementById("netbank_supplier_name_info").value= str;
  strx = justNumberNF(document.getElementById("amount").value);
  document.all["expense_vat_amount1"].value = NetbankToDollarsAndCents(strx * (24/124)) ;
  document.getElementById("amount").value=NetbankToDollarsAndCents(strx);
  document.getElementById("expense_amount1").value = document.getElementById("amount").value;

  document.getElementById("expense_amount2").value = '';
  document.getElementById("expense_account2").value= '';
  //document.getElementById("expense_vat_flag2").value= '';
  document.getElementById("expense_vat_amount2").value= '';
  document.getElementById("expense_amount3").value = '';
  document.getElementById("expense_account3").value= '';
  //.getElementById("expense_vat_flag3").value= '';
  document.getElementById("expense_vat_amount3").value= '';
  document.getElementById("expense_amount4").value = '';
  document.getElementById("expense_account4").value= '';
  //document.getElementById("expense_vat_flag4").value= '';
  document.getElementById("expense_vat_amount4").value= '';
  document.getElementById("expense_amount5").value = '';
  document.getElementById("expense_account5").value= '';
  //document.getElementById("expense_vat_flag5").value= '';
  document.getElementById("expense_vat_amount5").value= '';
  str = values_array[8] ;
  str = str.split(";sp;").join(" ");
  if (str=="2"){
   document.frmName.ButtonSelPeriodisering1.disabled=false;
   document.frmName.ButtonSelPeriodisering1.click();
  }
  winName.close();
 }
}

//Pass Data Back to original window
function f_popup_return(as_Value)
{
 var l_return = new Array(1);
 l_return[0] = as_Value;

 f_set_home_value(l_return);
}

function justNumberNF(val){
        val = (val==null) ? 0 : val;
        // check if a number, otherwise try taking out non-number characters.
        if (isNaN(val)) {
           var newVal = parseFloat(val.replace(/[^\d\.\-]/g, '.'));
   // check if still not a number. Might be undefined, '', etc., so just replace with 0.
           return (isNaN(newVal) ? 0 : newVal);
        }
        // return 0 in place of infinite numbers.
        else if (!isFinite(val)) { return 0; }

        return val;
};
function NetbankToDollarsAndCents(n) { 
 var s = "" + Math.round(n * 100) / 100  ;
 var i = s.indexOf('.') ;
 if (i < 0) {return s + ",00" } ;
 var t = s.substring(0, i + 1) + s.substring(i + 1, i + 3) ;
 if (i + 2 == s.length) {t += "0"} ;
 return t.replace('.',',') ;
 }

2 个答案:

答案 0 :(得分:2)

1)我真的建议使用jQuery来做这类事情。你基本上将一大堆元素默认为空字符串,而不是必须编写所有这些,你可以给所有输入元素一个虚假的类名(比如'inputClass'),然后在jQuery中编写以下代码:

$('.inputClass').val('');

让所有元素都得到更新,无论他们的ID如何(事实上,你甚至不需要一个!)。您目前拥有的代码几乎无法读取,维护起来很痛苦。

2)Firefox和IE有一个已知的“怪癖” - 如果你的名字是元素的name属性,而不是元素id属性,那么IE会发现它与getElementById调用一样好,而Firefox则不会。检查以确保这些名称实际上是元素的id属性。

答案 1 :(得分:0)

我带给你jQuery这可以为你节省很多,可能就像

一样简单
$("#form .fieldsclass").val("");