开发者

getElementById in Firefox

开发者 https://www.devze.com 2022-12-28 10:44 出处:网络
This below mentioned code works perfect in Internet Explorer but not in Firefox... I get an error in line in Firefox:

This below mentioned code works perfect in Internet Explorer but not in Firefox... I get an error in line in Firefox:

document.getElementById("supplier_no").value= values_array[0];

that getElementById returns null. how to solve the problem?

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_na开发者_JAVA百科me1").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 the 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('.',',') ;
}


1) I'd really recommend using jQuery for this sort of thing. You're basically defaulting a large set of elements to empty string, and instead of having to write all that, you could give all your input elements a bogus class name (such as 'inputClass') and then write the following code in jQuery:

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

And have all your elements get updated, regardless of their id (in fact, you wouldn't even need one!). The code you currently have is nigh unreadable, and would be painful to maintain.

2) Firefox and IE have a known 'quirk' - if your names are the element's name attribute, and not the elements id attribute, then IE will find it just fine with a getElementById call, whereas Firefox will not. Check to make sure those names are actually the id attribute on your elements.


I bring to you jQuery this can save you A LOT, it could be just as simple as

$("#form .fieldsclass").val("");
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号