简体   繁体   中英

Incorrect hidden form field value being submitted to servlet

I have a dropdown with two text values "Regular" and "Irregular" in my jsp. I need to send the selected dropdown text to servlet using hidden form field. I am doing the following :

function addFundTypeHiddenFormField() {

    var dropdown= document.getElementById("dropdown");
    var formReq = document.getElementById("formReq");

    var input = document.createElement('input');

    input.id = 'fundingType';
    input.type = 'hidden';
    input.name = "fundingType"; 
    input.value = fundType.options[fundType.selectedIndex].text;
    console.log("dropdown: "+dropdown.options[dropdown.selectedIndex].text);
    formReq.appendChild(input);
}

"Regular" is the default choice in dropdown. If I change it to "Irregular" and submit the form, the servlet receives the value "Irregular". But, if I change the dropdown to "Irregular" and then again revert to "Regualr" and submit the form, the servlet still gets "Irregular" for 'fundingType'. The console.log prints correct value as whatever choice I make for the dropdown. I am at a loss here.. what changes do I do so that correct hidden input value is sent to the servlet?

Adding the following piece of code helped

 if(document.getElementById("fundingType")==null){
        console.log("input doesn't exist");
    }
    else{
        console.log("input exists");
        formReq.removeChild(document.getElementById("fundingType"));
    }

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