不正确的隐藏表单字段值正在提交给servlet

时间:2018-09-13 19:03:05

标签: javascript java html

我在jsp中有一个带有两个文本值“ Regular”和“ Irregular”的下拉列表。我需要使用隐藏的表单字段将所选的下拉文本发送到servlet。我正在执行以下操作:

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);
}

“常规”是下拉菜单中的默认选项。如果我将其更改为“ Irregular”并提交表单,则Servlet会收到值“ Irregular”。但是,如果我将下拉列表更改为“ Irregular”,然后再次还原为“ Regualr”并提交表单,则servlet对于“ fundingType”仍然会获得“ Irregular”。 console.log会打印正确的值,作为我为下拉列表所做的任何选择。我在这里不知所措..我该怎么做才能将正确的隐藏输入值发送到servlet?

1 个答案:

答案 0 :(得分:1)

添加以下代码有帮助

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