从HTML表单的下拉列表中选择其他选项时不保存文本

时间:2013-01-24 12:40:45

标签: java html jsp

我使用以下代码以html格式开发下拉列表。

<html:select property="complaintCategory" onchange="showfield(this.options[this.selectedIndex].value)" >
<html:option value="option1">option1</html:option>
<html:option value="option2">option2</html:option>
<html:option value="option3">option3</html:option>
<html:option value="Other">Other</html:option>
</html:select>
<div id="div1"></div>

我想创建一个文本框,以便用户在选择其他选项时可以编写内容。函数showField(name)正在创建新的文本框。

function showfield(name)
    {
        if(name=='Other')document.getElementById('div1').innerHTML='<input type="text" name="complaintCategory" />';
        else document.getElementById('div1').innerHTML='';
    }

我面临的问题是,当我从下拉列表中选择“其他”选项然后在其上写一些内容时,它不保存文本,它仅保存为值其他。我想将文本框中写的文本作为complaintCategory传递。非常感谢别人的帮助,我被困住了。

提前致谢...

1 个答案:

答案 0 :(得分:0)

(我正在假设<html:select property="complaintCategory"将输出<select name="complaintCategory"。通常有助于将问题缩小到服务器端代码或客户端代码并仅提供其中一个。)

当您提交表单(选择其他表格后)后,您将拥有同名的 2 元素。

无论您使用哪种服务器端代码来阅读提交的表单数据,它似乎都采用第一个值。

或者:

  1. 修改您的服务器端代码以获取第二个值(我不知道您用来解析表单数据的API,因此我无法告诉您具体内容)
  2. 使用其他名称和if语句调用生成的输入。
  3. e.g。

    客户方:

    if(name=='Other')document.getElementById('div1').innerHTML='<input type="text" name="otherComplaintCategory" />';
    

    服务器端(伪代码):

    IF complaintCategory is "Other":
    THEN complaintCategory = otherComplaintCategory