从< select>生成键值对字段,使用jQuery [英] Generating key-value pairs from <select> field, using jQuery

查看:78
本文介绍了从< select>生成键值对字段,使用jQuery的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个< select> 字段。使用jQuery,我想以编程方式从select中的选项中获取值和名称,并创建一个包含每个选项字段的键值对的变量。

I have a <select> field. Using jQuery, I want get programmatically the value and the name from the option inside the select, and create a variable that contain the key-value pair of each option field.

例如,从这开始:

<select>
   <option value="option1">item1 </option>
   <option value="option2">item2</option>
</select>

我想生成vabiable:

I want generate the vabiable:

var obj = {
  "option1": "item1",
  "option2": "item2"
};


推荐答案

var obj = {}; //create an object
$("select").children().each(function () {
    obj[this.value] = this.innerHTML
})

工作示范

这篇关于从&lt; select&gt;生成键值对字段,使用jQuery的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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