简体   繁体   中英

JavaScript Chrome vs IE

I have very simple JavaScript code that copies values from one form into another. It works perfectly in Chrome, but in IE not everything is being copied. The "state field" would not copy. The state field is actually "select" field. Here is the function:

function FillMemberInfo(f){

      if(f.chkMemberInfo.checked == true) {
        f.member_firstname1.value = f.client_first_name.value;
        f.member_lastname1.value = f.client_last_name.value;
        f.member_address1.value = f.client_address.value;
        f.member_city1.value = f.client_city.value;
        f.member_state1.value = f.client_state.value;
        f.member_zip1.value = f.client_zip.value;
      } else {
        f.member_firstname1.value = "";
        f.member_lastname1.value = "";
        f.member_address1.value = "";
        f.member_city1.value = "";
        f.member_state1.value = "";
        f.member_zip1.value = "";

      }
    }

You can't directly set .value on a <select> unless the browser is clever enough to do that.

Try something like this:

function setSelectValue(sel,val) {
    var opts = sel.options, l = opts.length, i;
    for( i=0; i<l; i++) {
        if( opts[i].value == val) {
            sel.selectedIndex = i;
            return;
        }
    }
}

Then you can do:

setSelectValue(f.member_state1, f.client_state.value);

It should be noted that still older browsers don't support reading .value from a <select> , and you should ideally do:

f.client_state.options[f.client_state.selectedIndex].value;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM