是否同时发送选项框的值和键作为参数? [英] Sending both the value and key of an option box as parameters?

查看:39
本文介绍了是否同时发送选项框的值和键作为参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

提交表格时,我想同时发送选项框的值和键.我觉得这应该很简单,但是我不确定该怎么做.以下是我的表单中的片段,以演示我所引用的内容:

I want to send both the value and key of the option box when I submit a form. I feel like this should be pretty simple, but I'm unsure how to do it. Below is a snippet from my form to demonstrate what I'm referencing:

<form name='form' onSubmit="return checkForm();" action="../servlet/AccountRequest">

<select name="type1">
   <option value="1">Option A</option>
   <option value="2">Option B</option>
</select>

<br/><input type="button" id="Submit" onclick="checkForm(this.form)" value="Request" />
<input type="reset" value="Cancel"/>
</form>

在正常情况下,如果我在下拉框中选择选项A",则希望发送该值或"1".但是,我实际上要发送选择的值AND键,在这种情况下,都是"1"和"Option A".

​ In a normal scenario, if I selected "Option A" in the drop-down box, I would want to send the value, or "1". However, I want to actually send the value AND key of the selection, in this case both "1" and "Option A".

在我的情况下,我调用一个checkForm()JavaScript函数来验证表单输入(还有其他字段,例如名字,姓氏,电子邮件地址和密码),然后将参数转发到Java类(AccountRequest) ).我确定单击请求"按钮时,有一种方法可以将密钥存储为变量,我只是不知道该怎么做.

In my case, I call a checkForm() JavaScript function that validates form input (there are other fields, like First Name, Last Name, Email Address, and Password), which then forwards the parameters to a Java class (AccountRequest). I'm sure there is a way to store the key as a variable when the "Request" button is clicked, I just don't know how.

任何帮助将不胜感激!

推荐答案

这可能不是您所期望的方法,但是您可以将键/值对作为您的值发送,并在服务器端收到它时对其进行解析.

It might not be the approach you were expecting, but you could send the key/value pair as your value and parse it when you receive it server-side.

<select name="type1">
   <option value="1,Option A">Option A</option>
   <option value="2,Option B">Option B</option>
</select>

这篇关于是否同时发送选项框的值和键作为参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆