我的 jsp 中有一个包含两个文本值“常规”和“不规则”的下拉列表。我需要使用隐藏表单字段将选定的下拉文本发送到 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);
}
“常规”是下拉列表中的默认选项。如果我将其更改为“不规则”并提交表单,则 servlet 会收到值“不规则”。但是,如果我将下拉列表更改为“不规则”,然后再次恢复为“常规”并提交表单,servlet 的“fundingType”仍然为“不规则”。无论我为下拉列表做出什么选择,console.log 都会打印正确的值。我在这里不知所措..我需要做什么更改才能将正确的隐藏输入值发送到 servlet?
最佳答案
添加以下代码有帮助
if(document.getElementById("fundingType")==null){
console.log("input doesn't exist");
}
else{
console.log("input exists");
formReq.removeChild(document.getElementById("fundingType"));
}
关于javascript - 提交给 servlet 的隐藏表单字段值不正确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52320222/