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

标签 javascript java html

我的 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/

相关文章:

javascript - 带有无声 ajax 调用的 PayPal 快速结帐安全性

java - For-each 未正确迭代

html - Bootstrap jumbotron 全宽和带背景图像的窗口高度

javascript - 在可滚动容器中缩放居中的 div

java - 如何调用具有此类原型(prototype)的java函数?

javascript - 如何在单击相应按钮时将特殊符号附加到文本框

javascript - 使用 appendChild 到当前聚焦的元素

javascript - jQuery Validation form后卡住,继续提交

javascript - 使用以下内容解析时,第一个值返回 "undefined"

java - 使用动态别名准备 ibatis 查询