简体   繁体   English

在Node.js / Express中获取选择标签选项索引

[英]Get Select Tag option Index in Node.js/Express

When submitting a POST request with a form in Node.js/Express 使用Node.js / Express中的表单提交POST请求时

Eg, 例如,

<select name="selectname">
  <option value="value1">Value 1</option> 
  <option value="value2" selected>Value 2</option>
  <option value="value3">Value 3</option>
</select>

The value that is passed to req.body.selectname is "Value 2" --- however is there a way to tell the form to pass the value "value2" instead? 传递给req.body.selectname的值是“值2” ---但是有没有办法告诉表单传递值“值2”呢?

Edit: 编辑:

value2 is the default sent. value2 默认发送。 You may get "Value 2" if your value attributes are not correctly set such as the following: 如果您的值属性设置不正确,则可能会得到“值2”,例如:

<select name="selectname">
  <option>Value 1</option> 
  <option selected>Value 2</option>
  <option>Value 3</option>
</select>

When I submitted the request I was receiving "Value 2" --- instead of "value2". 提交请求时,我收到的是“值2” ---而不是“值2”。

I was only receiving "Value 2" because I had 我只收到“值2”,因为我有

<option selected> Value 2</option> 

instead of 代替

<option value="value2" selected> Value 2</option>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM